I am trying to translate a program I have in Java into ruby and having
trouble translating this for loop
for(int i = 0; a[i] < x;i++)
I tried this
for i in a[i]...x
but it produce some errors. Any help will be appreciated. Thank you.
numbers.each do |num|
break unless num < max
puts num
end
···
On Sat, Apr 16, 2011 at 1:27 PM, Daniel Johnson <zaldivar1841@gmail.com>wrote:
I am trying to translate a program I have in Java into ruby and having
trouble translating this for loop
for(int i = 0; a[i] < x;i++)
I tried this
for i in a[i]...x
but it produce some errors. Any help will be appreciated. Thank you.
I am trying to translate a program I have in Java into ruby and having
trouble translating this for loop
for(int i = 0; a[i] < x;i++)
I tried this
for i in a[i]...x
but it produce some errors. Any help will be appreciated. Thank you.
Ruby has a for-in loop:
a = [2, 4, 1, 9, 6]
x = 7
for num in a
break if num > x
puts num
end
But a for-in loop calls each(), so ruby programmers just use each()
directly:
That Ruby is almost certainly not the best way to translate the Java
loop, but the best way really depends on what you are doing in the
loop.
···
On Sat, Apr 16, 2011 at 11:27 AM, Daniel Johnson <zaldivar1841@gmail.com> wrote:
I am trying to translate a program I have in Java into ruby and having
trouble translating this for loop
for(int i = 0; a[i] < x;i++)
I tried this
for i in a[i]...x
but it produce some errors. Any help will be appreciated. Thank you.
I guess, depending on your needs, you might want the index as well, in which
case
numbers.each do |num|
becomes
numbers.each_with_index do |num, index|
···
On Sat, Apr 16, 2011 at 1:36 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Sat, Apr 16, 2011 at 1:27 PM, Daniel Johnson <zaldivar1841@gmail.com>wrote:
I am trying to translate a program I have in Java into ruby and having
trouble translating this for loop
for(int i = 0; a[i] < x;i++)
I tried this
for i in a[i]...x
but it produce some errors. Any help will be appreciated. Thank you.