Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use parameter-less functions in Ruby 1.9.x?

So I'm working through the Ruby Koans, and I've encountered an issue that I think is specific to ruby 1.9.x.

def test_calling_global_methods_without_parentheses

    result = my_global_method 2, 3
    assert_equal 5, result 
end

I get this:

james@tristan:~/code/ruby_projects/ruby_koans$ rake
(in /home/james/code/ruby_projects/ruby_koans)
cd koans
/home/james/.rvm/rubies/ruby-1.9.2-p180/bin/ruby path_to_enlightenment.rb
/home/james/code/ruby_projects/ruby_koans/koans/about_methods.rb:21:in `eval': (eval):1: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' (SyntaxError)
assert_equal 5, my_global_method 2, 3
                                  ^
    from /home/james/code/ruby_projects/ruby_koans/koans/about_methods.rb:21:in `test_sometimes_missing_parentheses_are_ambiguous'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:377:in `meditate'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:449:in `block in walk'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:460:in `block (3 levels) in each_step'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:458:in `each'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:458:in `block (2 levels) in each_step'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `each'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `each_with_index'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:457:in `block in each_step'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:455:in `catch'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:455:in `each_step'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:448:in `walk'
    from /home/james/code/ruby_projects/ruby_koans/koans/edgecase.rb:470:in `block in <top (required)>'
rake aborted!
Command failed with status (1): [/home/james/.rvm/rubies/ruby-1.9.2-p180/bi...]
/home/james/code/ruby_projects/ruby_koans/Rakefile:86:in `block in <top (required)>'
(See full trace by running task with --trace)
james@tristan:~/code/ruby_projects/ruby_koans$

I've looked at a few different repositories on GitHub that claim to have completed the Koans recently (In the past two months), and I've only seen the answer (First code snippet) that I used. So, is it something with my code, my Ruby install or something else?

like image 778
jrg Avatar asked Mar 17 '11 23:03

jrg


3 Answers

The error you're getting is not from the code you listed; it's from the code underneath it. See line 20 of the related file. The notes say:

NOTE: We are Using eval below because the example code considered to be syntactically invalid

like image 70
Michelle Tilley Avatar answered Oct 24 '22 08:10

Michelle Tilley


I don't know why, but the code is being evaluated like this:

def test_calling_global_methods_without_parentheses
  assert_equal 5, my_global_method 2, 3
end

The problem is that this is ambiguos, can mean assert_equal(5, my_global_method(2, 3)) or assert_equal(5, my_global_method(2), 3). In this specific case, you have to use parentheses.

like image 20
Guilherme Bernal Avatar answered Oct 24 '22 08:10

Guilherme Bernal


Don't forget to remove the space between the method call and the first parameter.

Do this

eval "assert_equal 5, my_global_method(2,3)"

and not

eval "assert_equal 5, my_global_method (2,3)" #beware of the space!

like image 24
H.Rabiee Avatar answered Oct 24 '22 10:10

H.Rabiee