# Modifying FIX::\* method

**URL:** <https://rubytalk.org/t/modifying-fix-method/45545>\
**Category:** ruby-talk\
**Created:** [31 March 2008 17:58 UTC](https://rubytalk.org/t/modifying-fix-method/45545 "2008-03-31T17:58:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [31 March 2008 17:58 UTC](https://rubytalk.org/t/modifying-fix-method/45545/1 "2008-03-31T17:58:30Z")

</div>

I have a class such as

Class Foo  
&nbsp;&nbsp;def initialize(x)  
&nbsp;&nbsp;&nbsp;&nbsp;@x = x  
&nbsp;&nbsp;end  
&nbsp;&nbsp;attr\_read :x  
end

I want to be able to multiply a NUMERIC object by my Foo object and have  
it return a new Foo object with all the instance variables multiplied by  
the Numeric object. For example:

y = Foo.new(3)  
z = 2\*y  
print z.x =\> 6

or

z = 2.0\*y  
print z.x =\> 6.0

I'm thinking I have to modify the "\*" method for all the subclasses of  
the NUMERIC class. Is this correct? Or am I missing something.

I know I can put a "\*" method in my Foo class, and get it to do things  
like "y\*2", but how do I get "2\*y"?

Thanks in advance.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [31 March 2008 18:24 UTC](https://rubytalk.org/t/modifying-fix-method/45545/2 "2008-03-31T18:24:46Z")

</div>

The proper way is to implement and enforce "y \* 2". Is there any  
specific reason you want people to be able to do "2 \* y" ?

Jason

> **···**
>
> On Mon, Mar 31, 2008 at 1:58 PM, Alex DeCaria \<alex.decaria@millersville.edu\> wrote:
> 
> > I have a class such as
> > 
> > Class Foo  
> > &nbsp;&nbsp;def initialize(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@x = x  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;attr\_read :x  
> > end
> > 
> > I want to be able to multiply a NUMERIC object by my Foo object and have  
> > it return a new Foo object with all the instance variables multiplied by  
> > the Numeric object. For example:
> > 
> > y = Foo.new(3)  
> > z = 2\*y  
> > print z.x =\> 6
> > 
> > or
> > 
> > z = 2.0\*y  
> > print z.x =\> 6.0
> > 
> > I'm thinking I have to modify the "\*" method for all the subclasses of  
> > the NUMERIC class. Is this correct? Or am I missing something.
> > 
> > I know I can put a "\*" method in my Foo class, and get it to do things  
> > like "y\*2", but how do I get "2\*y"?
> > 
> > Thanks in advance.  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Adam\_Shelly](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Adam\_Shelly](https://rubytalk.org/u/Adam_Shelly)\
**Post date:** [31 March 2008 18:27 UTC](https://rubytalk.org/t/modifying-fix-method/45545/3 "2008-03-31T18:27:19Z")

</div>

> I want to be able to multiply a NUMERIC object by my Foo object and have  
> it return a new Foo object with all the instance variables multiplied by  
> the Numeric object.

[...]

> I'm thinking I have to modify the "\*" method for all the subclasses of  
> the NUMERIC class. Is this correct? Or am I missing something.

You don't need to modify Numeric, you just need to tell it how to  
coerce your object into a number.

irb(main):057:0\* class Foo  
irb(main):058:1\> def initialize(x)  
irb(main):059:2\> @x=x  
irb(main):060:2\> end  
irb(main):061:1\> def coerce(n)  
irb(main):062:2\> [n,@x]  
irb(main):063:2\> end  
irb(main):064:1\> end  
=\> nil  
irb(main):065:0\> 2 + Foo.new(3)  
=\> 5  
irb(main):066:0\> f=Foo.new(4)  
=\> #\<Foo:0x283b13c @x=4\>  
irb(main):067:0\> 8/f  
=\> 2

-Adam

> **···**
>
> On 3/31/08, Alex DeCaria \<alex.decaria@millersville.edu\> wrote:

---

<div class="post-metadata">

**Author:** ![Shawn\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/9e8a1a/32.png) [@Shawn\_Anderson](https://rubytalk.org/u/Shawn_Anderson)\
**Post date:** [31 March 2008 18:35 UTC](https://rubytalk.org/t/modifying-fix-method/45545/4 "2008-03-31T18:35:08Z")

</div>

Adam,  
I believe the OP wanted a Foo object returned, not a Fixnum.

/Shawn

> **···**
>
> On Mon, Mar 31, 2008 at 11:27 AM, Adam Shelly \<adam.shelly@gmail.com\> wrote:
> 
> > On 3/31/08, Alex DeCaria \<alex.decaria@millersville.edu\> wrote:  
> > \> I want to be able to multiply a NUMERIC object by my Foo object and have  
> > \> it return a new Foo object with all the instance variables multiplied by  
> > \> the Numeric object.  
> > [...]  
> > \> I'm thinking I have to modify the "\*" method for all the subclasses of  
> > \> the NUMERIC class. Is this correct? Or am I missing something.  
> > \>
> > 
> > You don't need to modify Numeric, you just need to tell it how to  
> > coerce your object into a number.
> > 
> > irb(main):057:0\* class Foo  
> > irb(main):058:1\> def initialize(x)  
> > irb(main):059:2\> @x=x  
> > irb(main):060:2\> end  
> > irb(main):061:1\> def coerce(n)  
> > irb(main):062:2\> [n,@x]  
> > irb(main):063:2\> end  
> > irb(main):064:1\> end  
> > =\> nil  
> > irb(main):065:0\> 2 + Foo.new(3)  
> > =\> 5  
> > irb(main):066:0\> f=Foo.new(4)  
> > =\> #\<Foo:0x283b13c @x=4\>  
> > irb(main):067:0\> 8/f  
> > =\> 2
> > 
> > -Adam

---

<div class="post-metadata">

**Author:** ![Adam\_Shelly](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Adam\_Shelly](https://rubytalk.org/u/Adam_Shelly)\
**Post date:** [31 March 2008 18:37 UTC](https://rubytalk.org/t/modifying-fix-method/45545/5 "2008-03-31T18:37:57Z")

</div>

Oops, I didn't read your message closely enough. Maybe Jason is  
right, and you shouldn't even consider doing this, but something like  
the following should work:

class Foo  
&nbsp;&nbsp;attr\_reader :x,:y  
&nbsp;&nbsp;def initialize(x,y)  
&nbsp;&nbsp;&nbsp;&nbsp;@x=x  
&nbsp;&nbsp;&nbsp;&nbsp;@y=y  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def coerce(n)  
&nbsp;&nbsp;&nbsp;&nbsp;[Foo.new(n,n),self]  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def +(other)  
&nbsp;&nbsp;&nbsp;&nbsp;Foo.new(@x+other.x,@y+other.y)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def /(other)  
&nbsp;&nbsp;&nbsp;&nbsp;Foo.new(@x/other.x,@y/other.y)  
&nbsp;&nbsp;end

end

p (2 + Foo.new(3,0)) #=\> #\<Foo:0x27afcb8 @y=2, @x=5\>  
p f=Foo.new(4,10) #=\> #\<Foo:0x27afc68 @y=10, @x=4\>  
p 100/f #=\> #\<Foo:0x27afbdc @y=10, @x=25\>

> **···**
>
> On 3/31/08, Adam Shelly \<adam.shelly@gmail.com\> wrote:
> 
> > On 3/31/08, Alex DeCaria \<alex.decaria@millersville.edu\> wrote:  
> > \> I want to be able to multiply a NUMERIC object by my Foo object and have  
> > \> it return a new Foo object with all the instance variables multiplied by  
> > \> the Numeric object.  
> > [...]  
> > \> I'm thinking I have to modify the "\*" method for all the subclasses of  
> > \> the NUMERIC class. Is this correct? Or am I missing something.  
> > \>  
> > You don't need to modify Numeric, you just need to tell it how to  
> > coerce your object into a number.
