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
Alle 01:02, venerdì 26 gennaio 2007, Neville F. 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
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.
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’.
Alle 01:02, venerdì 26 gennaio 2007, Neville F. 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 ?
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 =
on method names, and gives you the nice calling syntax:
obj.x = 1 # prettified version of obj.x=(1)
The existence of the =-methods doesn’t pertain directly to instance
variables. You could write:
def call_me_ishmael @name = “Ishmael”
end
and you could also write:
def name=(name)
puts “You can’t name me!”
end
and so on.
Lots of =-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.
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.