Dividing inside iterations?

D

Dominic Son

Hi

Learning Ruby.

Is it possible to divide values while inside an iteration?

for example i'd like to do something like this:

h = [40,20,50,200]
h.each do |num,total| num / 24 = total
print "#{total} \n" end

as you'd expect, there is an error because of that '/' and '=' sign in
this iterator.

so since this isn't possible what would be the best way to do what i'm
trying to do? or perhaps my syntax is wrong

thank you very much.

Dominic
 
D

dblack

Hi --

Yes, but in assignement the variable goes on the left.
h = [40,20,50,200]
h.each do |num,total|

The "total" there doesn't do anything; you'd just want:

h.each do |num|
total = num / 24
print "#{total} \n"

Of course this could also be:

puts num / 24

:)


David
end

j`ey
http://www.eachmapinject.com

Hi

Learning Ruby.

Is it possible to divide values while inside an iteration?

for example i'd like to do something like this:

h = [40,20,50,200]
h.each do |num,total| num / 24 = total
print "#{total} \n" end

as you'd expect, there is an error because of that '/' and '=' sign in
this iterator.

so since this isn't possible what would be the best way to do what i'm
trying to do? or perhaps my syntax is wrong

thank you very much.

Dominic

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 

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
474,209
Messages
2,571,088
Members
47,686
Latest member
sparada

Latest Threads

Top