# Simple mix-in example

**URL:** <https://www.ruby-forum.com/t/simple-mix-in-example/75056>\
**Category:** Ruby\
**Created:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056 "2006-11-07T11:08:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056/1 "2006-11-07T11:08:32Z")

</div>

Hi,

can we consider this is a mix-in newbie example ? We are giving a new  
feature to method \> on our Mixin class.

class Mixin  
def initialize(a)  
@a = a  
end  
def \>(arg)  
puts “Yep!” if @a \> arg  
end  
end

test = Mixin.new(3)  
test \> 1 # -\> Yep!  
test.\> 1 # -\> Yep!  
test.\>(1) # -\> Yep!  
puts “Yep, too!” if 2 \> 1 # -\> Yep, too!  
puts “not now!” if teste \< 1 # -\> \*

- undefined method `\<’ for #\<Mixin:0xb7d35858 @a=3\> (NoMethodError)

Works as expected. Thanks,

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056/2 "2006-11-07T11:08:33Z")

</div>

On Tuesday 07 November 2006 09:35, pedro mg wrote:

> ```
> puts "Yep!" if @a > arg
> 
> ```
> 
> - undefined method `\<’ for #\<Mixin:0xb7d35858 @a=3\> (NoMethodError)
> 
> Works as expected. Thanks,

I wouldn’t call it Mixin, that could give some conflict with the concept  
of  
mixins (i.e. modules)  
maybe something like that:

class Compare  
attr\_accessor :a

def initialize a  
@a = a  
end

def \> arg  
puts “Yep!” if @a \> arg  
end  
end

^manveru

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056/3 "2006-11-07T11:08:37Z")

</div>

Em Tue, 07 Nov 2006 09:45:12 +0900, Michael F. escreveu:

> end  
> Thanks. Done 😉

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056/4 "2006-11-07T11:08:48Z")

</div>

On Tue, 07 Nov 2006 00:33:00 +0000, pedro mg wrote:

> ```
> puts "Yep!" if @a > arg
> 
> ```
> 
> - undefined method `\<’ for #\<Mixin:0xb7d35858 @a=3\> (NoMethodError)
> 
> Works as expected. Thanks,

Yuck.

if test \> 1  
puts “Yes”  
else  
puts “No”  
end

will print out  
Yep!  
No

You need to define the \> operator as follows:  
def \>(arg)  
puts “Yep!” if @a \> arg  
@a \> arg  
end  
so that it returns a proper value

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [November 7, 2006, 11:08am UTC](https://www.ruby-forum.com/t/simple-mix-in-example/75056/5 "2006-11-07T11:08:53Z")

</div>

Em Tue, 07 Nov 2006 04:39:06 +0000, Ken B. escreveu:

> if test \> 1  
> puts “Yes”  
> else  
> puts “No”  
> end

thanks, this was really just a very simple test in wich i controled the  
values.

> You need to define the \> operator as follows:  
> def \>(arg)  
> puts “Yep!” if @a \> arg  
> @a \> arg  
> end  
> so that it returns a proper value

i didn’t even wanted a return value, but you’re right, even the  
simple examples look better with a little more effort 😉
