[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby language question

Neville Franks

1/26/2007 12:02:00 AM

Hi, I've been working on adding Ruby language support to our IDE "ED for
Windows" the past few weeks and am stuck understanding the marked
statement in this code:

module Mod
def size
@size
end
def size=(val) # what does this mean
@size = val
end
end

None of the reference material I've found so far shows this. I saw it in
the documentation for: attr(symbol, writable=false) => nil

Thanks,
Neville

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

11 Answers

Jeremy McAnally

1/26/2007 12:08:00 AM

0

That is setting an attribute, which essentially means you can do this:

class MyClass
include Mod
end

x = MyClass.new
x.size = 9
=> 9
x.size
=> 9

--Jeremy

On 1/25/07, Neville Franks <subs@surfulater.com> wrote:
> Hi, I've been working on adding Ruby language support to our IDE "ED for
> Windows" the past few weeks and am stuck understanding the marked
> statement in this code:
>
> module Mod
> def size
> @size
> end
> def size=(val) # what does this mean
> @size = val
> end
> end
>
> None of the reference material I've found so far shows this. I saw it in
> the documentation for: attr(symbol, writable=false) => nil
>
> Thanks,
> Neville
>
> --
> Posted via http://www.ruby-....
>
>


--
My free Ruby e-book:
http://www.humblelittlerubybook...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Stefano Crocco

1/26/2007 12:11:00 AM

0

Alle 01:02, venerdì 26 gennaio 2007, Neville Franks ha scritto:
> Hi, I've been working on adding Ruby language support to our IDE "ED for
> Windows" the past few weeks and am stuck understanding the marked
> statement in this code:
>
> module Mod
> def size
> @size
> end
> def size=(val) # what does this mean
> @size = val
> end
> end
>
> None of the reference material I've found so far shows this. I saw it in
> the documentation for: attr(symbol, writable=false) => nil
>
> Thanks,
> Neville

def size=(val) is simply the definition of a method called size=, which
accepts the parameter val. It's the ruby way of allowing write access to an
instance variable. Its role is similar to C++ setter functions. When you
write

an_object.var=2

I hope this answers your question.

Stefano

Neville Franks

1/26/2007 12:13:00 AM

0

Jeremy,
Thanks for the prompt reply. I understand what the code is doing but
don't understand the meaning/use of the line:
def size=(val)
in particular the =(val) part.

Doesn't the lime:
@size = val
perform the actual assignment.

---
Neville Franks, http://www.g... http://www.surf...


Jeremy McAnally wrote:
> That is setting an attribute, which essentially means you can do this:
>
> class MyClass
> include Mod
> end
>
> x = MyClass.new
> x.size = 9
> => 9
> x.size
> => 9
>
> --Jeremy
>
> On 1/25/07, Neville Franks <subs@surfulater.com> wrote:
>> end
>>
>>
>
>
> --
> My free Ruby e-book:
> http://www.humblelittlerubybook...
>
> My blogs:
> http://www.mrneigh...
> http://www.rubyinpra...


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

Gavin Kistner

1/26/2007 12:15:00 AM

0


On Jan 25, 5:02 pm, Neville Franks <s...@surfulater.com> wrote:
> def size=(val) # what does this mean
> @size = val
> end

Methods whose names end in an '=' character can be called with a space
between the method name and the '=' character. Combined with the
ability to leave parentheses off a method call, it looks very much like
you're assigning to an attribute, when you're calling a method:

irb(main):001:0> class Foo; def a=( b ); p b; end; end
=> nil
irb(main):002:0> f = Foo.new
=> #<Foo:0x52f468>
irb(main):003:0> f.a=(1)
1
=> 1
irb(main):004:0> f.a =( 2 )
2
=> 2
irb(main):005:0> f.a= 3
3
=> 3
irb(main):006:0> f.a = 4
4
=> 4

One thing to note - the single argument passed to the function under
this syntax is also always the return value. (In the above, the #p
method returns nil, but you can see that the return value of the method
is the 'rvalue' for the 'assignment'.

Neville Franks

1/26/2007 12:17:00 AM

0

Stefano Crocco wrote:
> Alle 01:02, venerdì 26 gennaio 2007, Neville Franks ha scritto:
>> end
>> end
>>
>> None of the reference material I've found so far shows this. I saw it in
>> the documentation for: attr(symbol, writable=false) => nil
>>
>> Thanks,
>> Neville
>
> def size=(val) is simply the definition of a method called size=, which
> accepts the parameter val. It's the ruby way of allowing write access to
> an
> instance variable. Its role is similar to C++ setter functions. When you
> write
>
> an_object.var=2
>
> I hope this answers your question.
>
> Stefano

Stefano, thanks that makes sense. I guess it is a more like operator
overloading in C++, maybe!

Would you expect to see the method listed as: size= in a Class Viewer or
just size ?



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

dblack

1/26/2007 12:22:00 AM

0

dblack

1/26/2007 12:23:00 AM

0

Gavin Kistner

1/26/2007 12:23:00 AM

0

On Jan 25, 5:17 pm, Neville Franks <s...@surfulater.com> wrote:
> Would you expect to see the method listed as: size= in a Class Viewer or
> just size ?

The former, as Ruby does it:

irb(main):001:0> class Foo; def a=( b ); end; end
=> nil
irb(main):002:0> Foo.instance_methods(false)
=> ["a="]

Neville Franks

1/26/2007 12:33:00 AM

0

Thanks all for your help. I wish I'd found this forum a few weeks back.
Trying to write a syntax parser, highlighter and code browser for any
language is challenging, but for Ruby even more so. I've written around
30 language parsers for ED over the years and Ruby is amongst the most
complex.

Back to my original code snippet. Isn't @size = val redundant?

PS. If anyone is interested in trying the forthcoming Ruby language
support in ED please drop me an e-mail. It would be great to get some
feedback from hard-core Ruby developers.

---
Neville Franks, http://www.g... http://www.surf...

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

dblack

1/26/2007 12:44:00 AM

0