LoginSignup
0
1

More than 5 years have passed since last update.

Relationship of space and argument

Posted at

Let's see the following code.

def times_two(arg1);
  puts arg1 * 2;
end

def sum(arg1, arg2);
  puts arg1 + arg2;
end
times_two 5
times_two(5)
times_two (5)
sum 1, 2
sum(1, 2)
sum (1, 2)

when call the functions above, which returns error?

Result...

10
10
10
3
3
SyntaxError: (irb):14: syntax error, unexpected ',', expecting ')'
sum (1, 2)
       ^

Why??

Because there is a space between function and parenthesis of arguments, Ruby parser find it as a single argument, but it is invalid, so returned error.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1