the_prole the_prole - 5 days ago 5
Ruby Question

When to use at symbol to reference an instance variable and when not to?

If I have a custom class with a method that prints the instance variable

var
should I reference
var
using an
at symbol @
?

TL;DR: should I use the method
puts_reader
or
puts_ivar
and why?

class Foo
attr_reader :var

def initialize
@var = "foobar"
end

def puts_reader
puts var
end

def puts_ivar
puts @var
end
end

Answer

In your code you can use just var because the attr_reader that is something like

def var
  @var
end
Comments