This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Chris Hudson
over 11 years

iterating with .times method, "It looks like you didn't print out the string 'Ruby!' 30 times."

It’s telling me there are the “wrong number of arguments” when I write

30.times print “Ruby!”

Any advice?

Answer 50a222bd797b866e30001981

7 votes

Permalink

@Chris you see the “wrong number of arguments” error because you forgot the { } to delimit the block you need to pass to .times. The .times method doesn’t accept any arguments (only a block), so if a word follows it, it assumes that it’s supposed to be an argument and throws that error.

30.times { 
  # do something
} 

# or, the equivalent

30.times do 
  # do something
end
points
Submitted by Alex J
over 11 years

Answer 55f18aeae39efef749000390

0 votes

Permalink

the syntax is

30.times do |x| print “Ruby!” end

points
Submitted by adekayor
over 8 years

Answer 50a15956197c9e1467007103

-10 votes

Permalink

i = 0 loop do i+= 1 print “Ruby!” break if i >=30 end

is the right answer

points
Submitted by Sherry Icy
over 11 years

1 comments

kakubei almost 11 years

This is definitely NOT the Ruby way to do it. Alex J’s answer is much better. In Ruby there’s rarely any need to use i = 0, etc loops.