Profile image of systemAce78303
Submitted by systemAce78303
over 12 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
Profile image of fanaugen
Submitted by fanaugen
over 12 years

Answer 55f18aeae39efef749000390

0 votes

Permalink

the syntax is

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

Profile image of adekayor
Submitted by adekayor
almost 10 years

Answer 50a15956197c9e1467007103

-10 votes

Permalink

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

is the right answer

Profile image of SherryIcy
Submitted by SherryIcy
over 12 years

1 comments

Profile image of kakubei
Submitted by kakubei
about 12 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.