R
Rajinder Yadav
I am trying to learn the correct way to enumerate in Ruby, a few things are
missing in my 1st stab at this. How do I use mixin with Enumerable to rewrite
the class I have below? I can't seem to find a concrete example online?
I know I am also missing code to override <=> if I wanted to do sorting, are the
other overrides I am missing?
How do I make my code more elegant, Thanks!
class Vowels
def initialize
@index = 0
@list = %w(a e i o u)
end
def each
while( @index < @list.size )
if @index >= @list.size
yield nil
exit
else
yield @list[@index]
@index += 1
end
end
end
def first
@index=0
end
end
obj = Vowels.new
obj.each { |x| puts x }
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good ~ Share Freely
missing in my 1st stab at this. How do I use mixin with Enumerable to rewrite
the class I have below? I can't seem to find a concrete example online?
I know I am also missing code to override <=> if I wanted to do sorting, are the
other overrides I am missing?
How do I make my code more elegant, Thanks!
class Vowels
def initialize
@index = 0
@list = %w(a e i o u)
end
def each
while( @index < @list.size )
if @index >= @list.size
yield nil
exit
else
yield @list[@index]
@index += 1
end
end
end
def first
@index=0
end
end
obj = Vowels.new
obj.each { |x| puts x }
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good ~ Share Freely