1

This is probably one line answer fot Ruby pros. I am getting syntax error:

unexpected tLBRACE  at line 1

I believe this is closely related to the issue in described here but I am not able to figure whats wrong in my case.

Could some please pin point whats the issue ? Thank you for your time.

def user_profile_picture(user,  size: [50, 50], type: :square, style: 'img-polaroid', opts: {})
    tag :img,
        { width: ("#{size[0]}px" if size),
          height: ("#{size[1]}px" if size),
          src: facebook_profile_picture(user, type),
          alt: '',
          class: [('verified' if user.class == User.model_name && user.facebook_verified?), style].compact.join(' ')
        }.merge(opts)
  end

ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.0]

****Stack trace****

 SyntaxError - /Developer/rails-workspace/roommate/app/helpers/users_helper.rb:7: syntax error, unexpected tLABEL
...er_profile_picture(user,  size: [50, 50], type: :square, sty...
...                               ^
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:7: syntax error, unexpected ',', expecting keyword_end
..._picture(user,  size: [50, 50], type: :square, style: 'img-p...
...                               ^
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:7: syntax error, unexpected ',', expecting keyword_end
...:square, style: 'img-polaroid', opts: {})
...                               ^
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:10: syntax error, unexpected ',', expecting keyword_end
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:11: syntax error, unexpected ',', expecting keyword_end
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:12: syntax error, unexpected ',', expecting keyword_end
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:15: syntax error, unexpected '}', expecting keyword_end
        }.merge(opts)
         ^
/Developer/rails-workspace/roommate/app/helpers/users_helper.rb:127: syntax error, unexpected keyword_end, expecting $end:
  app/helpers/users_helper.rb:7:in `'
  (gem) activesupport-3.2.9/lib/active_support/dependencies.rb:469:in `block in load_file'
  (gem) activesupport-3.2.9/lib/active_support/dependencies.rb:639:in `new_constants_in'
  (gem) activesupport-3.2.9/lib/active_support/dependencies.rb:468:in `load_file'
Community
  • 1
  • 1
Sudhakar
  • 2,904
  • 8
  • 33
  • 47

1 Answers1

3

My bet is that you're using a earlier Ruby version than 2.0.

At the first line, you've defined the method with keyword arguments. The error you've obtained is typically the one when working with an earlier Ruby version such as 1.9. The hash syntax 'size:' It's an unexpected syntax in a list of parameter in those versions.

In Ruby 1.9, you should define the method as following:

def user_profile_picture(user,  size=[50, 50], type=:square, style='img-polaroid', opts={})
  #...
end
toch
  • 3,905
  • 2
  • 25
  • 34
  • in IRB, it works fine: 1.9.3-p327 :007 > {size: [50, 50], type: :square, style: 'img-polaroid', opts: {}} => {:size=>[50, 50], :type=>:square, :style=>"img-polaroid", :opts=>{}} – Sudhakar Apr 11 '13 at 08:28
  • @Sudhakar But because you declare a hash not a method, it's not the same. I've seen your stacktrace, it's exactly what I obtain with Ruby 1.9. In Ruby 2.0, no syntax error. – toch Apr 11 '13 at 08:33
  • Thanks @toch you were right. Using your code fixed the issue. Alos trying now to upgrade ruby to 2.0.0 – Sudhakar Apr 11 '13 at 08:45