[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Return object

Dmitry N Orlov

11/9/2003 4:38:00 AM

(Inspired by Test Driven Development of Kent Bek)
How Can I return from a class method object of the class ? Like this:
class Dollar
attr_reader :amount
def initialize(amount)
@amount = amount
end
def times(multiplier)
return Dollar.new(@amount * multiplier) #??????????
end
end


4 Answers

gabriele renzi

11/9/2003 11:12:00 AM

0

il Sun, 9 Nov 2003 09:37:42 +0500, "Dmitry N Orlov"
<NOSPAM_orlovdn@cipro.uz> ha scritto::

>(Inspired by Test Driven Development of Kent Bek)
>How Can I return from a class method object of the class ? Like this:
>class Dollar
> attr_reader :amount
> def initialize(amount)
> @amount = amount
> end
> def times(multiplier)
> return Dollar.new(@amount * multiplier) #??????????
> end
>end
>

well, times is not a class method:
>> class Dollar
>> attr_reader :amount
>> def initialize(amount)
>> @amount = amount
>> end
>> def times(multiplier)
>> return Dollar.new(@amount * multiplier) #??????????
>> end
>> end
=> nil
>> Dollar.times(1)
NoMethodError: undefined method `times' for Dollar:Class
from (irb):10

and @amount is an instance variable:
>> a=Dollar.new(10)
=> #<Dollar:0x2806c68 @amount=10>
>> a.times(10)
=> #<Dollar:0x2804178 @amount=100>

as you can see everything is fine :)
but probably you may want to use

def Dollar.times
....
end
or
def self.times
....
end
and @@amount ?

orlovdn

11/10/2003 7:11:00 AM

0

gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>...
I just want return object of the class from object-method times(). See
TDD, please, to understand me :)

orlovdn

11/10/2003 1:33:00 PM

0

orlovdn@rambler.ru (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google.com>...
> gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>...
> I just want return object of the class from object-method times(). See
> TDD, please, to understand me :)
Sorry. It's fine

class Dollar
attr_reader :amount
def initialize(amount)
@amount = amount
end
def times(multiplier)
return Dollar.new(@amount * multiplier) #??????????
end
end

five = Dollar.new(5)
product = five.times(6)
#Now product is a Instance of Dollar
p product.inspect
p five.inspect
p (product.times(10).inspect)
p product.inspect
p five.inspect

#<Dollar:0x2787810 @amount=30>
#<Dollar:0x27878e8 @amount=5>
#<Dollar:0x2787750 @amount=300>
#<Dollar:0x2787810 @amount=30>
#<Dollar:0x27878e8 @amount=5>

gabriele renzi

11/10/2003 8:18:00 PM

0

il 10 Nov 2003 05:32:34 -0800, orlovdn@rambler.ru (Dmitry N Orlov) ha
scritto::

>orlovdn@rambler.ru (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google.com>...
>> gabriele renzi <surrender_it@remove.yahoo.it> wrote in message news:<lv7sqv86nbca1r62cf2hud3u952uv103ov@4ax.com>...
>> I just want return object of the class from object-method times(). See
>> TDD, please, to understand me :)
>Sorry. It's fine

well, I supposed this :)

BTW you don't need to do
p object.inspect
p object
is enough

Kernel#p calls argument#inspect by itself, that's what is for :)