Confused with objects in Ruby

Ruby Newbie is here,

I understand that this is all an object in Ruby, and I was not sure about the understanding of variables. Variables basically give a reference to objects (correct me if I am wrong). During the training video, the teacher made a demonstration, which was as follows:

(IRB)

a = 100
==> 100
b = a
==> 100
b
==> 100

This part that I get has a general meaning.

Then he did

a = 50
==> 50
b
==> 100

If B should point to what was set to 100, why does b still point to 100 if a is now set to 50?

+3
source share
3 answers

, 100 . , a 100, b = a, b 100 a 100, a.

, :

irb> puts 100.object_id
=> 201

, ?

Ruby, . , , Ruby:

, , , ( , ), . ( , ).

Ruby , . , . , , . method_missing. , , , .

, , Person, bubba :

class Person
  attr_accessor :dob, :name
  def age
    years = Time.now.year - @dob.year
    puts "You are #{years} year#{"s" if years != 1} old"
  end
  def feed
    puts "nom, nom, nom"
  end
end
bubba = Person.new
bubba.name = "Bubba"
bubba.dob = Time.new(1983,9,26)

: alt text

, , /? , , Ruby, - Class. , , , , . def self.method_name, , /.

, ? (aka singleton, eigen, ghost class), .

Person, , :

def bubba.drive_pickup
  puts "Yee-haw!"
end

, , Person. : alt text

, bubba, . .

, , , singleton . , , .

: x Z y Z x y? , , . , , - , .

Ruby (, ):
http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4

Dave Thomas Ruby Pragmatic, .

P.S. -, , , ; .

+9

b = a : b a. : b , . a

a=50

b .

, (Charles = a Dog). Charlie . ( = ). , , .

, , , (Charles = a Goldfish). Ruby - Charlie.

, , .

+2

, ( ) , . , b = a, : " b , ". , "a" "b" , . , .

+1
source

Source: https://habr.com/ru/post/1789368/


All Articles