I’m confused about attr_writer. I thought it worked like this:
class Foo
attr_writer :bar
def do_stuff
puts @bar
end
end
foo = Foo.new
foo.bar = ‘baz’
foo.do_stuff # gives nil error
Can somebody enlighten me?
Thanks,
Joe
I’m confused about attr_writer. I thought it worked like this:
class Foo
attr_writer :bar
def do_stuff
puts @bar
end
end
foo = Foo.new
foo.bar = ‘baz’
foo.do_stuff # gives nil error
Can somebody enlighten me?
Thanks,
Joe
Joe wrote:
I’m confused about attr_writer. I thought it worked like this:
class Foo
attr_writer :bardef do_stuff
puts @bar
end
endfoo = Foo.new
foo.bar = ‘baz’
foo.do_stuff # gives nil errorCan somebody enlighten me?
This is perfectly valid. If this is not your actual code,
could you post that instead? It might be something else
entirely.
Thanks,
Joe
E
Curious. I just copied, pasted and ran your code. It produces the string
‘baz’. I don’t get an error.
“Joe” [email protected] wrote in message
news:[email protected]…
Are you running it in irb? As someone else mentioned… you’ll get a
“nil” message as that’s the return value of puts.
-Jeff
Joe wrote:
foo = Foo.new
foo.bar = ‘baz’
foo.do_stuff # gives nil errorCan somebody enlighten me?
Thanks,
Joe
puts returns nil
lopex
Turns out I had a bug elsewhere in my code and it does in fact work as
advertised. Thanks for the responses.
Joe
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs