Forum: Ruby Ruby language question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ab5d0d66db7c70b5634be1e28e614383?d=identicon&s=25 Neville Franks (nevf)
on 2007-01-26 01:02
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
6d3c187a8b3ef53b08e3e7e8572c4fea?d=identicon&s=25 Jeremy McAnally (Guest)
on 2007-01-26 01:08
(Received via mailing list)
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.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-01-26 01:11
(Received via mailing list)
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
Ab5d0d66db7c70b5634be1e28e614383?d=identicon&s=25 Neville Franks (nevf)
on 2007-01-26 01:13
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.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 Franks <subs@surfulater.com> wrote:
>>      end
>>
>>
>
>
> --
> My free Ruby e-book:
> http://www.humblelittlerubybook.com/book/
>
> My blogs:
> http://www.mrneighborly.com/
> http://www.rubyinpractice.com/
Ab5d0d66db7c70b5634be1e28e614383?d=identicon&s=25 Neville Franks (nevf)
on 2007-01-26 01:17
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 ?
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-26 01:20
(Received via mailing list)
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'.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-26 01:22
(Received via mailing list)
Hi --

On Fri, 26 Jan 2007, Stefano Crocco 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-26 01:23
(Received via mailing list)
Hi --

On Fri, 26 Jan 2007, Neville Franks 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
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-26 01:25
(Received via mailing list)
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="]
Ab5d0d66db7c70b5634be1e28e614383?d=identicon&s=25 Neville Franks (nevf)
on 2007-01-26 01:33
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.getsoft.com http://www.surfulater.com
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-26 01:44
(Received via mailing list)
Hi --

On Fri, 26 Jan 2007, Neville Franks wrote:

> 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
Ab5d0d66db7c70b5634be1e28e614383?d=identicon&s=25 Neville Franks (nevf)
on 2007-01-26 01:53
unknown wrote:
> Hi --
>
> On Fri, 26 Jan 2007, Neville Franks wrote:
>
>> 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.
>
>

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.
> David
This topic is locked and can not be replied to.