Using attr_writer?

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? :slight_smile:

Thanks,
Joe

Joe wrote:

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? :slight_smile:

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.

best wishes
Huw C.

Bitwise Magazine
www.bitwisemag.com
Dark Neon Ltd.

“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 error

Can somebody enlighten me? :slight_smile:

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. :slight_smile:

Joe