rubyattr-accessor

Strange behave of ruby attr_accessor


I have this block of code:

class CallMe
  attr_accessor :a, :b, :c

  def self.start(*args)
     self.new(*args).get_answer
  end

  def initialize(a,b,c)
    @a = a
    @b = b
    @c = c
  end

  def get_answer 
    if c
      b = nil
    else
      return b 
    end
   end
end
answer = CallMe.start(1,2,nil)

Why when i run it in irb i always get answer = nil even logic case is get b value that is 2


Solution

  • Variable Hoisting effect is used in many languages. For Ruby it is described in the official documentation:

    The local variable is created when the parser encounters the assignment, not when the assignment occurs

    So, get_answer method creates local variable b regardless to value of c. And assigns local variable b to nil while creating. Then get_answer returns local variable b which is always nil.

    Correct method:

    def get_answer
      c ? self.b = nil : b
    end