Ask a Question related to Ruby, Design and Development.
-
Dmitry N Orlov #1
Return object
(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
Dmitry N Orlov Guest
-
Can I return a query object from a file upload usingFlex 3?
Hi, I am using Flex 3 and I would be quite grateful if someone can assist me here. I am trying to find out if I can return a query back to flex... -
return object instances
hello, since I'm new to the world of object oriented programming I hope I use the right terms for the concerned things, but I'll try. I was... -
Object property with different return types
I have a control 'MyControl' with a property of type 'MyObject'. MyObject has a property 'MyField' that is a enum type. I want the type of enum... -
Can you return a dictionary object from a function?
"Mark Schupp" <mschupp@ielearning.com> wrote in message news:OwPxqtxPDHA.1748@TK2MSFTNGP11.phx.gbl... doh! -
gabriele renzi #2
Re: Return object
il Sun, 9 Nov 2003 09:37:42 +0500, "Dmitry N Orlov"
<NOSPAM_orlovdn@cipro.uz> ha scritto::
well, times is not a class method:>(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
>=> nil>> class Dollar
>> attr_reader :amount
>> def initialize(amount)
>> @amount = amount
>> end
>> def times(multiplier)
>> return Dollar.new(@amount * multiplier) #??????????
>> end
>> endNoMethodError: undefined method `times' for Dollar:Class>> Dollar.times(1)
from (irb):10
and @amount is an instance variable:=> #<Dollar:0x2806c68 @amount=10>>> a=Dollar.new(10)=> #<Dollar:0x2804178 @amount=100>>> a.times(10)
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 ?
gabriele renzi Guest
-
Dmitry N Orlov #3
Re: Return object
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 :)
Dmitry N Orlov Guest
-
Dmitry N Orlov #4
Re: Return object
[email]orlovdn@rambler.ru[/email] (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google. com>...
Sorry. It's fine> 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 :)
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>
Dmitry N Orlov Guest
-
gabriele renzi #5
Re: Return object
il 10 Nov 2003 05:32:34 -0800, [email]orlovdn@rambler.ru[/email] (Dmitry N Orlov) ha
scritto::
well, I supposed this :)>orlovdn@rambler.ru (Dmitry N Orlov) wrote in message news:<45323c22.0311092310.56710a4e@posting.google. com>...>Sorry. It's fine>> 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 :)
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 :)
gabriele renzi Guest



Reply With Quote

