Forum: Ruby Using attr_writer?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe (Guest)
on 2006-02-26 22:34
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
E. Saynatkari (Guest)
on 2006-02-26 22:42
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? :)

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
Marcin MielżyÅ?ski (Guest)
on 2006-02-26 22:44
(Received via mailing list)
Joe wrote:
> foo = Foo.new
> foo.bar = 'baz'
> foo.do_stuff # gives nil error
>
> Can somebody enlighten me? :)
>
> Thanks,
> Joe
>

puts returns nil


lopex
Huw C. (Guest)
on 2006-02-26 22:50
(Received via mailing list)
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...
Jeff McNeil (Guest)
on 2006-02-26 22:59
(Received via mailing list)
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 (Guest)
on 2006-02-27 00:06
Turns out I had a bug elsewhere in my code and it does in fact work as
advertised.  Thanks for the responses. :)

Joe
This topic is locked and can not be replied to.