Sha256: 62d564b5512ff95dba5dcbef4d5e9b7c105e6d5067ef548e7f12a26c108c7ff6

Contents?: true

Size: 987 Bytes

Versions: 20

Compression:

Stored size: 987 Bytes

Contents

# frozen_string_literal: true

class Object
  # Returns a hash with string keys that maps instance variable names without "@" to their
  # corresponding values.
  #
  #   class C
  #     def initialize(x, y)
  #       @x, @y = x, y
  #     end
  #   end
  #
  #   C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
  def instance_values
    instance_variables.to_h do |ivar|
      [ivar[1..-1].freeze, instance_variable_get(ivar)]
    end
  end

  if Symbol.method_defined?(:name) # RUBY_VERSION >= "3.0"
    # Returns an array of instance variable names as strings including "@".
    #
    #   class C
    #     def initialize(x, y)
    #       @x, @y = x, y
    #     end
    #   end
    #
    #   C.new(0, 1).instance_variable_names # => ["@y", "@x"]
    def instance_variable_names
      instance_variables.map(&:name)
    end
  else
    def instance_variable_names
      variables = instance_variables
      variables.map! { |s| s.to_s.freeze }
      variables
    end
  end
end

Version data entries

20 entries across 20 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.5.1 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.5 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.4.2 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.4.1 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.4 lib/active_support/core_ext/object/instance_variables.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/instance_variables.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/instance_variables.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.3.4 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.3.2 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.3.1 lib/active_support/core_ext/object/instance_variables.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.3 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.2 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.1 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.0 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.0.rc2 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.0.rc1 lib/active_support/core_ext/object/instance_variables.rb
activesupport-7.1.0.beta1 lib/active_support/core_ext/object/instance_variables.rb