Ruby language question


#1

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


#2

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 F. removed_email_address@domain.invalid wrote:

 end


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

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


#3

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


#4

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 F., http://www.getsoft.com http://www.surfulater.com

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 F. removed_email_address@domain.invalid wrote:

 end


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

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


#5

On Jan 25, 5:02 pm, Neville F. removed_email_address@domain.invalid 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’.


#6

Stefano C. wrote:

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 ?


#7

Hi –

On Fri, 26 Jan 2007, Neville F. wrote:

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 ?

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

David


#8

Hi –

On Fri, 26 Jan 2007, Stefano C. wrote:

   @size = val

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.

David


#9

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 F., http://www.getsoft.com http://www.surfulater.com


#10

On Jan 25, 5:17 pm, Neville F. removed_email_address@domain.invalid 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=”]


#11

Hi –

On Fri, 26 Jan 2007, Neville F. wrote:

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

Not if you want @size set to val :slight_smile:

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


#12

unknown wrote:

Hi –

On Fri, 26 Jan 2007, Neville F. wrote:

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

Not if you want @size set to val :slight_smile:

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. :slight_smile:
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.

David