fluent interface program in Ruby
Posted
by intern
on Stack Overflow
See other posts from Stack Overflow
or by intern
Published on 2010-05-28T05:09:48Z
Indexed on
2010/05/28
5:11 UTC
Read the original article
Hit count: 258
we have made the following code and trying to run it.
class Numeric def gram self end alias_method :grams, :gram
def of(name) ingredient = Ingredient.new(name) ingredient.quantity=self return ingredient end end
class Ingredient def initialize(n) @@name= n end
def quantity=(o)
@@quantity = o
return @@quantity
end
def name
return @@name
end
def quantity
return @@quantity
end
end
e= 42.grams.of("Test") a= Ingredient.new("Testjio") puts e.quantity a.quantity=90 puts a.quantity puts e.quantity
the problem which we are facing in it is that the output of puts a.quantity puts e.quantity is same even when the objects are different. what we observed is that second object i.e 'a' is replacing the value of the first object i.e. 'e'. the output is coming out to be 42 90 90 but the output required is 42 90 42
can anyone suggest why is it happening? it is not replacing the object as object id's are different..only the values of the objects are replaced.
© Stack Overflow or respective owner