iterate Array

S

Sijo Kg

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
 
C

Craig Demyanovich

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
 
S

Sijo Kg

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

Sijo
 
R

Robert Klemme

2008/5/16 Sijo Kg said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top