[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using attr_writer?

Igor France

2/26/2006 8:35:00 PM

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

--
Posted via http://www.ruby-....


5 Answers

Marcin Mielzynski

2/26/2006 8:40:00 PM

0

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? :)
>
> Thanks,
> Joe
>

puts returns nil


lopex

Yuu

2/26/2006 8:42:00 PM

0

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

--
Posted via http://www.ruby-....


Huw Collingbourne

2/26/2006 8:44:00 PM

0

Curious. I just copied, pasted and ran your code. It produces the string
'baz'. I don't get an error.

best wishes
Huw Collingbourne
================================
Bitwise Magazine
www.bitwisemag.com
Dark Neon Ltd.
================================


"Joe" <joe@yahoo.com> wrote in message
news:2df9ba1d3b2002133798b2b6d3d429fc@ruby-forum.com...
> 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
>
> --
> Posted via http://www.ruby-....
>
>


Jeff McNeil

2/26/2006 8:57:00 PM

0

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

On Feb 26, 2006, at 3:48 PM, Huw Collingbourne wrote:

> Curious. I just copied, pasted and ran your code. It produces the
> string
> 'baz'. I don't get an error.
>
> best wishes
> Huw Collingbourne
> ================================
> Bitwise Magazine
> www.bitwisemag.com
> Dark Neon Ltd.
> ================================
>
>
> "Joe" <joe@yahoo.com> wrote in message
> news:2df9ba1d3b2002133798b2b6d3d429fc@ruby-forum.com...
>> 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
>>
>> --
>> Posted via http://www.ruby-....
>>
>>
>
>
>



Igor France

2/26/2006 10:06:00 PM

0

Turns out I had a bug elsewhere in my code and it does in fact work as
advertised. Thanks for the responses. :)

Joe

--
Posted via http://www.ruby-....