0

I was reading the source code of a gem "activerecord-postgres-earthdistance".

While running the migration script, it threw an error on the following method

def order_by_distance lat, lng, order: "ASC"

It gave an error for order: "ASC"

syntax error, unexpected tLABEL

Isn't this valid Ruby syntax?

2 Answers 2

2

Ruby 2.0 supports keywords arguments

[5] pry(main)> def bar(a: "name", b: "fem"); puts a,b end
[6] pry(main)> bar(a: "John", b: "Male")
John
Male
[7] pry(main)> bar("John", "Male")
ArgumentError: wrong number of arguments (2 for 0)
from (pry):5:in `bar'

However the above is not valid in 1.9 see below:

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
[2] pry(main)> def bar(a: "name", b: "fem"); puts a,b end
SyntaxError: unexpected ',', expecting $end
def bar(a: "name", b: "fem"); puts a,b end
              ^
[2] pry(main)> def bar(a: "name"); puts a end
SyntaxError: unexpected ')', expecting $end
def bar(a: "name"); puts a end
              ^

For better understanding you can read here and here

Sign up to request clarification or add additional context in comments.

Comments

0
def order_by_distance(lat, lng, hash={})
  puts hash[:order]
end

=> order_by_distance(lat, lng, order: "ASC")
=> "ASC"

use hash arguments in ruby

8 Comments

I tried that too def foo( a, b: "ASC") puts "runs" end It gives the error syntax error, unexpected ')', expecting $end
what ruby version you use?
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
def foo( a, b: "ASC") puts "runs" end inline?
for inline use need def foo( a, b: "ASC"); puts "runs"; end
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.