Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't Ruby return an array without "return"?

Tags:

ruby

def foo
  1,2
end

causes syntax error "unexpected ',', expecting keyword_end"

I'd think this is valid Ruby. What's wrong?

like image 264
slowpoison Avatar asked May 11 '26 07:05

slowpoison


1 Answers

You're not returning an array.

You should have this:

def foo
  [1, 2]
end

Ruby isn't expecting a comma (,) because it isn't valid syntax. Integers in a simple array should be surrounded by brackets as well as delineated by a comma.

like image 192
Noah Avatar answered May 16 '26 10:05

Noah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!