[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

iterate Array

Sijo Kg

5/16/2008 11:46:00 AM

Hi
I have created an array like
def checkdata(data)
@passed_effort= data.split(':').collect! {|n| n}
hours = @passed_effort[0]
minutes = @passed_effort[1]
@error_checked_value=Array.new
if hours =~ /\D/ || minutes =~ /\D/
@error_checked_value << 'nonnumeric'
end
if minutes.to_i > 59
@error_checked_value << 'notinrange'
end
return @error_checked_value
end

Now how can I iterate through this array
@error_checked_value.each do |val|
puts val
end

Is this correct?Or any other method?

Sijo
--
Posted via http://www.ruby-....

3 Answers

Craig Demyanovich

5/16/2008 12:37:00 PM

0

Using each is the most common way to iterate an array. Is there
something more than puts-ing each value that you want to do that
caused you to ask your question?

Regards,
Craig

Sijo Kg

5/16/2008 12:50:00 PM

0

Hi
I have a doubt regrding .collect That is why I asked tht.Is there any
such a method?

Sijo
--
Posted via http://www.ruby-....

Robert Klemme

5/16/2008 2:09:00 PM

0

2008/5/16 Sijo Kg <sijo@maxxion.com>:
> I have a doubt regrding .collect That is why I asked tht.Is there any
> such a method?

Yes, there is. But your usage of collect does not make any sense:

xxx.collect! {|n| n}

Using #collect or #map with this block is basically a noop because
elements will be replaced by themselves.

Kind regards

robert



--
use.inject do |as, often| as.you_can - without end