Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class setter

Reply
Thread Tools

Class setter

 
 
Ves Pasian
Guest
Posts: n/a
 
      11-26-2008
For the life of me I can figure out why the setter below returns "v" and
not nil

class A
def initialize(id)
@id = id
end

def id
@id
end

def id=(v)
nil
end
end

a = A.new(1)
a.id = 2
=> 2

Any idea?
TIA,
ves
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      11-26-2008
On Nov 26, 10:43=A0am, Ves Pasian <(E-Mail Removed)> wrote:
> For the life of me I can figure out why the setter below returns "v" and
> not nil
>
> class A
> =A0 def initialize(id)
> =A0 =A0 @id =3D id
> =A0 end
>
> =A0 def id
> =A0 =A0 @id
> =A0 end
>
> =A0 =A0def id=3D(v)
> =A0 =A0 =A0nil
> =A0 =A0end
> end
>
> a =3D A.new(1)
> a.id =3D 2
> =3D> 2


That's just the way setters work in Ruby. Any method ending in =3D will
return what you pass to it.
That way, it's analogous to setting regular variables, like x =3D y =3D z.

On a similar note, though you "configure" the class method .new with
the instance method #initialize, you can't make .new return something
other than an instance of the class just by the return value of
#initialize.

--
-yossef

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-26-2008
Yossef Mendelssohn wrote:
> On a similar note, though you "configure" the class method .new with
> the instance method #initialize, you can't make .new return something
> other than an instance of the class just by the return value of
> #initialize.


That's not really similar. The only connection between new and initialize is
that new calls initialize. There is no rule that a method that calls another
method should return the return value of that method (given that new calls
two methods, that'd be impossible anyway). So new returning the newly created
object even though it calls initialize is perfectly ordinary. The behaviour
of setters, however, is a special case.

--
NP: Sentenced - My Slowing Heart
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Yossef Mendelssohn
Guest
Posts: n/a
 
      11-26-2008
On Nov 26, 10:59=A0am, Sebastian Hungerecker <(E-Mail Removed)>
wrote:
> That's not really similar. The only connection between new and initialize=

is
> that new calls initialize. There is no rule that a method that calls anot=

her
> method should return the return value of that method (given that new call=

s
> two methods, that'd be impossible anyway). So new returning the newly cre=

ated
> object even though it calls initialize is perfectly ordinary. The behavio=

ur
> of setters, however, is a special case.


Good point and clarification. It just seems similar to me because
normal usage involves defining #initialize and calling .new, but it's
as you say -- one method calls the other and doesn't use that return
value.

--
-yossef

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
DataBinder setter method. =?Utf-8?B?VHUtVGhhY2g=?= ASP .Net 0 03-15-2005 11:23 PM
Tired of 100s of stupid Getter/Setter methods Timo Nentwig Java 75 01-18-2004 05:17 AM
JSP taglib: Unable to find setter method for attribute kaeli Java 5 01-05-2004 07:15 PM
May I do data type conversion within getter/setter? Raoul Markus Java 3 09-20-2003 06:30 PM



Advertisments