Ruby language question

N

Neville Franks

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
 
J

Jeremy McAnally

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

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


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

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
S

Stefano Crocco

Alle 01:02, venerd=C3=AC 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=3D(val) # what does this mean
@size =3D 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=3Dfalse) =3D> nil

Thanks,
Neville

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

an_object.var=3D2

I hope this answers your question.

Stefano
 
N

Neville Franks

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.
 
P

Phrogz

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'.
 
N

Neville Franks

Stefano said:
Alle 01:02, venerdì 26 gennaio 2007, Neville Franks ha scritto:

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 ?
 
D

dblack

---2049402039-982185925-1169770884=:24160
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-982185925-1169770884=:24160"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-982185925-1169770884=:24160
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Alle 01:02, venerd=C3=AC 26 gennaio 2007, Neville Franks ha scritto:

def size=3D(val) is simply the definition of a method called size=3D, whi= ch
accepts the parameter val. It's the ruby way of allowing write access to = an
instance variable.

I'd put a slightly different spin on it. Ruby lets you put a final =3D
on method names, and gives you the nice calling syntax:

obj.x =3D 1 # prettified version of obj.x=3D(1)

The existence of the =3D-methods doesn't pertain directly to instance
variables. You could write:

def call_me_ishmael
@name =3D "Ishmael"
end

and you could also write:

def name=3D(name)
puts "You can't name me!"
end

and so on.

Lots of =3D-methods (including all of those created with attr_writer)
involve writing to instance variables; but that's a conventioned
layered on top of the way it works, rather than being the way it works
itself.


David

--=20
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-982185925-1169770884=:24160--
---2049402039-982185925-1169770884=:24160--
 
D

dblack

---2049402039-1726411110-1169770956=:24160
Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1726411110-1169770956=:24160"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

---2049402039-1726411110-1169770956=:24160
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

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=3D in a Class Viewer o= r
just size ?

You wouldn't see size=3D listed as size; size and size=3D are different
methods. The =3D is part of the method name.


David

--=20
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
---2049402039-1726411110-1169770956=:24160--
---2049402039-1726411110-1169770956=:24160--
 
P

Phrogz

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="]
 
N

Neville Franks

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.
 
D

dblack

Hi --

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

Not if you want @size set to val :)

Consider this:

class C
def size=(val)
puts "Nice try!"
end
end

c = C.new
c.size = 20 # Nice try!

The instance variable @size is nowhere to be seen, and has not been
involved with any of this. size= is just a method name.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
N

Neville Franks

unknown said:
Hi --



Not if you want @size set to val :)

Consider this:

class C
def size=(val)
puts "Nice try!"
end
end

c = C.new
c.size = 20 # Nice try!

The instance variable @size is nowhere to be seen, and has not been
involved with any of this. size= is just a method name.

Ok I finally completely understand. :)
a) size= is the method name.
b) (val) is the method parameter
c) @size=val assigns val to @size.

You'll need to excuse this poor old C++ guy being a bit slow at times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top