1

Rails 4.1 & Ruby 2.0

Here's what I have in lib/global_methods.rb:

def admin_status? (id = nil)
  role_value = I18n.t 'admin_role'
  unless id
    current_user.role == role_value
  else
    determine_user_role (id, role_value)
  end
end

def determine_user_role (id, role_value)
  user = User.find_by id: id
  user.role == role_value
end

Here's what I have in application_controller.rb

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  require ('json')
  require ('csv')
  require ('global_methods')

end

I am getting the following error:

syntax error, unexpected ',', expecting ')'

and it points to line 7 in application_controller.rb as the culprit. If delete the functions from global_method.rb, I no longer get the error. I can't see the syntax problem. Any ideas?

Joseph Mouhanna
  • 69
  • 1
  • 1
  • 6
  • Not strictly related, but essentially nobody puts parens around the args to `require`. I'd recommend against it for readability. – Dave Newton May 17 '14 at 02:08
  • Yeah, I started out without parans, but at the point where I copied the code, I was about an hour into trying different things to diagnose the issue. I will revert to no parans. It is more readable – Joseph Mouhanna May 17 '14 at 03:04

2 Answers2

1

It's no longer valid (since 1.8.7 I believe) to call a multi-arg method with a space between the method name and any parenthesis:

~ >rvm 1.8.7
~ >ruby -ve'def bs(a,b) end; bs (1,2)'
ruby 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin12.5.0]
-e:1: warning: don't put space before argument parentheses
~ >rvm 2.1.1
~ >ruby -ve'def bs(a,b) end; bs (1,2)'
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]
-e:1: syntax error, unexpected ',', expecting ')'
def bs(a,b); end; bs (1,2)
                        ^
-e:1: warning: possibly useless use of a literal in void context
~ >ruby -ve'def bs(a,b) end; bs(1,2)'
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]

Here's another answer from StackOverflow that touches on the ruby grammer behind this.

Community
  • 1
  • 1
sshaw
  • 994
  • 6
  • 10
0

It's complaining about your I18n translation. When I rewrite your code, putting the definition of determine_user_role() prior to the definition of admin_status?() in global_methods.rb, and instead of doing the I18n translation of 'admin_role' just using a standard string, it works fine.

Unfortunately, I'm not intimately familiar with the i18n gem or how you need to have it plugged into and configured for your Rails environment, so I can't provide a complete answer for you here. There just isn't enough information in your question to know.

alpartis
  • 1,086
  • 14
  • 29
  • I18n is part of the core rails environment and there's not much to the configuration/setup. It is usable "out of the box" without any setup. The issue is not I18n either, it's that I had a space between the method name and the parenthesis surrounding the arguments. Ruby post 1.8 fails if you do that. Using I18n is as essential as writing functions for your code. – Joseph Mouhanna May 17 '14 at 03:12
  • For what it's worth, I ran the code outside Rails w/o the space. Trying to use the i18n translation, I got the reported syntax error. W/o that attempted translation it worked fine. So, depending on the environment you're in, 2 different things in your code could have produced the same error you got. It's also not helpful that the error report from the Ruby translator doesn't specify the actual offending line of code, but the reference to the required external file. – alpartis May 17 '14 at 05:42