Ruby variable as the same object (pointers?)

>> a = 5
=> 5
>> b = a
=> 5
>> b = 4
=> 4
>> a
=> 5

how can I set “b” to actually “a”, so in the example the variable a will also become four. thank.

+5
source share
7 answers
class Ref
  def initialize val
    @val = val
  end

  attr_accessor :val

  def to_s
    @val.to_s
  end
end

a = Ref.new(4)
b = a

puts a   #=> 4
puts b   #=> 4

a.val = 5

puts a   #=> 5
puts b   #=> 5

When you execute b = a, bpoints to the same object as a(they have the same object_id).

When you execute a = some_other_thing, a will point to another object, and bwill remain unchanged.

Fixnum, nil, true false object_id. (, , ..) object_id, (=).

:

a = 'abcd'
b = a

puts a  #=> abcd
puts b  #=> abcd

a.upcase!          # changing a

puts a  #=> ABCD
puts b  #=> ABCD

a = a.downcase     # assigning a

puts a  #=> abcd
puts b  #=> ABCD

:

a = [1]
b = a

p a  #=> [1]
p b  #=> [1]

a << 2            # changing a

p a  #=> [1, 2]
p b  #=> [1, 2]

a += [3]          # assigning a

p a  #=> [1, 2, 3]
p b  #=> [1, 2]
+5

. , .

:

a = 5 # Assign the value 5 to the variable named "a".
b = a # Assign the value in the variable "a" (5) to the variable "b".
b = 4 # Assign the value 4 to the variable named "b".
a # Retrieve the value stored in the variable named "a" (5).

. : .

+2

, , , . , -, ,

ruby-1.8.7-p334 :007 > class Wrapper
ruby-1.8.7-p334 :008?>   attr_accessor :number
ruby-1.8.7-p334 :009?>   def initialize(number)
ruby-1.8.7-p334 :010?>     @number = number
ruby-1.8.7-p334 :011?>   end
ruby-1.8.7-p334 :012?> end
 => nil 
ruby-1.8.7-p334 :013 > a = Wrapper.new(4)
 => #<Wrapper:0x100336db8 @number=4> 
ruby-1.8.7-p334 :014 > b = a
 => #<Wrapper:0x100336db8 @number=4> 
ruby-1.8.7-p334 :015 > a.number = 6
 => 6 
ruby-1.8.7-p334 :016 > a
 => #<Wrapper:0x100336db8 @number=6> 
ruby-1.8.7-p334 :017 > b
 => #<Wrapper:0x100336db8 @number=6> 
+1

:

a = [5]
b = a
b[0] = 4
puts a[0]  #=>  4

.

+1

.

>> a = 5
=> 5
>> a.object_id
=> 11
>> b = a
=> 5
>> b.object_id
=> 11
>> b = 4
=> 4
>> b.object_id
=> 9
>> a.object_id
=> 11
# We did change the Fixnum b Object.
>> Fixnum.superclass
=> Integer
>> Integer.superclass
=> Numeric
>> Numeric.superclass
=> Object
>> Object.superclass
=> BasicObject
>> BasicObject.superclass
=> nil

, Ruby.

+1

, , , , .

, , , proc , -.

:

def hash_that_will_change_later
  params = {}
  some_resource.on_change do
    params.replace {i: 'got changed'}
  end
  params
end
a = hash_that_will_change_later
=> {}
some_resource.trigger_change!
a
{i: 'got changed'}

, , / .

+1

Ruby. kluge... , eval , :

>> a = 5
=> 5
>> b = :a
=> :a
>> eval "#{b} = 4"
=> 4
>> eval "#{a}"
=> 4
>> eval "#{b}"
=> 4

, b :a, , eval:

>> b
=> :a
>> b + 1
NoMethodError: undefined method `+' for :a:Symbol

... , , . , binding ...

' ' Ruby?

@ Paul.s has an answer, if you can change the declaration point as a wrapper object, but if you can control the reference point, then here is the class BasicReferenceI tried:

class BasicReference
    def initialize(r,b)
        @r = r
        @b = b
        @val = eval "#{@r}", @b
    end

    def val=(rhs)
        @val = eval "#{@r} = #{rhs}", @b
    end

    def val
        @val
    end
end

a = 5

puts "Before basic reference"
puts "   the value of a is #{a}"

b = BasicReference.new(:a, binding)

b.val = 4

puts "After b.val = 4"
puts "   the value of a is #{a}"
puts "   the value of b.val is #{b.val}"

It is output:

Before basic reference
   the value of a is 5
After b.val = 4
   the value of a is 4
   the value of b.val is 4
0
source

All Articles