Using attr_writer?


#1

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


#2

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


#3

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” removed_email_address@domain.invalid wrote in message
news:removed_email_address@domain.invalid…


#4

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


#5

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


#6

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