syntax error, unexpected ',', expecting ')' - ruby

I just installed Ruby 1.9.2 after having used 1.8.7, as there is a feature I need. I had called many of my methods like this:
do_something (arg0, arg1)
With 1.9.2, i get the following error, syntax error, unexpected ',', expecting ')' and the fix seems to be:
do_something arg0, arg1
But this could take me hours to fix all the cases. Is there a way around this? Why is it an error in the first place? thanks

The extra space is the culprit. Use:
do_something(arg0, arg1)

Related

Ruby and ctioga2 issue

I am trying to run ctioga2 and I keep getting the same error:
bash-4.1$ ctioga2
/usr/bin/ctioga2:49:in `require': /usr/lib/ruby/site_ruby/1.8/ctioga2/plotmaker.rb:646: syntax error, unexpected ')' (SyntaxError)
) do |plotmaker, size, options|
^
/usr/lib/ruby/site_ruby/1.8/ctioga2/plotmaker.rb:646: syntax error, unexpected '|', expecting '='
/usr/lib/ruby/site_ruby/1.8/ctioga2/plotmaker.rb:867: syntax error, unexpected kEND, expecting $end
from /usr/bin/ctioga2:49
Do you guys know why this happens?
Not familiar with the code base exactly of tioga2. Based on the error message it was expecting an assignment (=) and ran into a | and )
This is a ruby-1.8 specific bug. It is now fixed in the git repository (commit: be1cc4b).
But stackoverflow is not the place to file bug reports for programs, you're lucky I stumbled upon this one.
For ctioga2, please use either the bug tracker at sourceforge: https://sourceforge.net/p/ctioga2/tickets/ or the one at github if you prefer: https://github.com/fourmond/ctioga2/issues/.

Seeing numbers in Ruby with r attached to the number

When you come across numbers that look like this in Ruby code, what does it mean?
1r
Or
1.0r
Tested in Ruby 1.8.6 up to 2.0.0 and it fails. with something like this:
>> 1r
SyntaxError: unexpected tIDENTIFIER, expecting end-of-input
and
>> 1.0r
SyntaxError: unexpected tIDENTIFIER, expecting end-of-input
Probably downvoted for not searching, or something, or not enough examples. Hopefully this clarifies. It is just Ruby Code, it doesn't have to be specifically anywhere, just has to be there.
That is a new feature for Decimal/Rational Literals in Ruby 2.1. See here: http://rkh.im/ruby-2.1 (search for "Decimal Literals")
0.1r #=> (1/10)
0.1r * 3 #=> (3/10)

Ruby: syntax error, unexpected tIDENTIFIER, expecting ')'

Working on exercise #26 of Learn Ruby The Hard Way -- correcting a ficticious programmer's bad code.
I've got most of it worked out, but can't even get to testing because I keep getting this syntax error:
syntax error, unexpected tIDENTIFIER, expecting ')'
...on this line:
sentence = "All good\tthings come to those who wait."
I thought that was always the way variables were declared? Since the error was listing parens, I tried those too -- around sentence (even though it made no sense), around the string (both with and without quotes), with the equals sign, without the equals sign...I'm not really sure what the issue is here.
Not always errors are on the same lines as interpreter says ;) So it would be better if you include some adjacent lines next time. But as I found these lines are:
puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
sentence = "All god\tthings come to those who weight."
words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)
From here we see that the line before your specified line doesn't have closing parenthesis ')'.

Multiline if else ruby

I know this questions has been touched on elsewhere, but I'm slightly confused about the proper syntax for multi-line (block?) if else statements in ruby.
As an example:
if condition then
do something
do somethingelse
do yetanotherthing
done
else
do acompletelyunrelatedthing
done
I understand that the then statement is required if multiple lines are used, but is the done before the else necessary? This seems like it would break out of the if...else context. When I do include this done I get:
syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'
When I don't include it I get:
syntax error, unexpected keyword_else, expecting keyword_end
Um... there is no done keyword in Ruby. Here is the correct syntax:
if condition
# do stuff
else
# do other stuff
end
The then keyword is not needed either.
The then keyword is only used if you need to put the whole thing on one line (to separate the condition from the action to take if true):
if condition then do_something else do_something_different end
If you don't want it all one line (usually you don't), the syntax is as in Doorknob's answer.

Weird syntax error when comparing x with x.method

Why this works fine:
t="
"+$<.read;puts t.reverse==t ?"YES":"NO"
but this:
t="
"+$<.read;puts t==t.reverse ?"YES":"NO"
says:
A.rb:2: syntax error, unexpected tCHAR, expecting $end
"+$<.read;puts t==t.reverse ?"YES":"NO"
^
I use ruby 1.9.2p290 (2011-07-09) [i386-mingw32].
Sample STDIN string is XX.\n...\n.XX\n.
Looks like Ruby is parsing the latter as a potential call to #reverse?. That ambiguity is removed when switched the other way. Adding parentheses around the conditional should allow it to go both ways.

Resources