Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5 Questions about instance variables

5 Questions about instance variables

What are instance variables? Why are instance variables confusing?

Enrique Carlos Mogollan

August 02, 2016
Tweet

More Decks by Enrique Carlos Mogollan

Other Decks in Programming

Transcript

  1. 1. What are instance variables? class User def initialize(name, email)

    @name = name @email = email end end puts User.new("Peter", "[email protected]").instance_variables @name @email
  2. class User def initialize(name, email) @name = name @email =

    email end def to_s "Name: #{@name}" End def send_notification notification.push(@name, @email) end end
  3. Why so many @? class User def initialize(name, email) @name

    = name @email = email end def to_s "Name: #{@name}" End def send_notification notification.push(@name, @email) end end
  4. Why so many @? class User ... def name @name

    end def email @email end def send_notification notification.push(name, email) end end
  5. Why so many @? class User ... attr_reader :name, :email

    def send_notification notification.push(name, email) end end
  6. class User attr_accessor :name, :email ... End > user =

    User.new("Peter Barker", "[email protected]") > puts user.name "Peter Barker" > user.name = "Peter Parker" > puts user "Peter Parker" Why so many @?
  7. Why are instance v@rs confusing? class User def initialize(name, dob)

    @name = name @dob = dob calculate_age end def to_s "Name #{@name}, Age: #{@age}" end private def calculate_age @age = dob_to_age(@dob) end end
  8. Why are instance v@rs confusing? class User attr_accessor :name, :dob,

    :age def initialize(name, dob) @name = name @dob = dob calculate_age end def to_s "Name #{name}, Age: #{age}" end private def calculate_age age = dob_to_age(dob) end end
  9. class User attr_accessor :name, :dob, :age def initialize(name, dob) @name

    = name @dob = dob calculate_age end def to_s "Name #{name}, Age: #{age}" end private def calculate_age age = dob_to_age(dob) # this is not an instance var end end
  10. class User def age @age end def age=(value) @age =

    value end def to_s "Name #{name}, Age: #{age}" end private def calculate_age age = dob_to_age(dob) # age is a local var end end
  11. class User def age @age end def age=(value) @age =

    value end def to_s "Name #{name}, Age: #{age}" end private def calculate_age self.age = dob_to_age(dob) # this sends a message to self end end
  12. class User def age @age end def age=(value) @age =

    value end def to_s "Name #{name}, Age: #{age}" end private def calculate_age @age = dob_to_age(dob) # ignores the method end end
  13. class User attr_accessor :age # Get the instance variable value

    def to_s "Age: #{age}" "Age: #{self.age}" "Age: #{@age}" end private # Set the instance variable value def calculate_age age = dob_to_age(dob) # creates a local var self.age = dob_to_age(dob) # sends a message @age = dob_to_age(dob) # ignores the method end end Why are instance v@rs confusing?
  14. class User def age @age end def age=(value) @age =

    value end def to_s "Name #{name}, Age: #{age}" end private def calculate_age age = dob_to_age(dob) # Why age = doesn’t # set the instance variable? end end
  15. That is how Ruby works… however 4. How does YARV

    works for v@riable assignations?
  16. compiled = RubyVM::InstructionSequence.compile(code) puts compiled.disasm A trick learned from “Ruby

    Under a Microscope” http://patshaughnessy.net/ruby-under-a-microscope Get YARV instructions
  17. # age = == disasm: <RubyVM::InstructionSequence:calculate_age@<compiled>>======= local table (size: 2,

    argc: 0 [opts: 0, rest: -1, post: 0, block: -1, keyword: 0@3] s1) [ 2] age 0000 trace 8 ( 13) 0002 trace 1 ( 14) 0004 putself 0005 opt_send_simple <callinfo!mid:calculate_age, argc:0, FCALL|VCALL|ARGS_SKIP> 0007 dup 0008 setlocal_OP__WC__0 2 0010 trace 16 ( 15) 0012 leave ( 14) # self.age = == disasm: <RubyVM::InstructionSequence:calculate_age@<compiled>>======= 0000 trace 8 ( 13) 0002 trace 1 ( 14) 0004 putnil 0005 putself 0006 putself 0007 opt_send_simple <callinfo!mid:calculate_age, argc:0, FCALL|VCALL|ARGS_SKIP> 0009 setn 2 0011 opt_send_simple <callinfo!mid:age=, argc:1, FCALL|ARGS_SKIP> 0013 pop 0014 trace 16 ( 15) 0016 leave ( 14) # @age = == disasm: <RubyVM::InstructionSequence:calculate_age@<compiled>>======= 0000 trace 8 ( 13) 0002 trace 1 ( 14) 0004 putself 0005 opt_send_simple <callinfo!mid:calculate_age, argc:0, FCALL|VCALL|ARGS_SKIP> 0007 dup 0008 setinstancevariable :@age, <is:0> 0011 trace 16 ( 15) 0013 leave
  18. age = calculate_age local table (size: 2, argc: ...) 0008

    setlocal_OP__WC__0 2 @age = calculate_age 0008 setinstancevariable :@age, <is:0> self.age = calculate_age 0011 opt_send_simple <callinfo!mid:age=, ...> How ruby parses v@riable setting?
  19. I send messages class User attr_accessor :name def to_s "Name

    #{name}, Age: #{age}" end ... private def age @age ||= dob_to_age(dob) end end