[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple mix-in example

pedro mg

11/7/2006 12:33:00 AM

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,
--
pedro mg
ruby 1.8.4 (2005-12-24) [i486-linux]
irb 0.9.5(05/04/13)
Linux xubuntu 2.6.17-10-generic #2 SMP i686 GNU/Linux
4 Answers

Michael Fellinger

11/7/2006 12:45:00 AM

0

On Tuesday 07 November 2006 09:35, pedro mg wrote:
> 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,

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

pedro mg

11/7/2006 1:06:00 AM

0

Em Tue, 07 Nov 2006 09:45:12 +0900, Michael Fellinger escreveu:
> class Compare
> attr_accessor :a
>
> def initialize a
> @a = a
> end
>
> def > arg
> puts "Yep!" if @a > arg
> end
> end

Thanks. Done ;)
--
pedro mg
ruby 1.8.4 (2005-12-24) [i486-linux]
irb 0.9.5(05/04/13)
Linux xubuntu 2.6.17-10-generic #2 SMP i686 GNU/Linux

Ken Bloom

11/7/2006 4:39:00 AM

0

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

> 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,

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
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

pedro mg

11/7/2006 5:48:00 AM

0

Em Tue, 07 Nov 2006 04:39:06 +0000, Ken Bloom 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 ;)

--
pedro mg
ruby 1.8.4 (2005-12-24) [i486-linux]
irb 0.9.5(05/04/13)
Linux xubuntu 2.6.17-10-generic #2 SMP i686 GNU/Linux