"Ipsum"} can be replaced by new_hash = {simon: "Talek", lorem: "Ipsum"} ...">

New Ruby 1.9 hash syntax

new_hash = {:simon => "Talek", :lorem => "Ipsum"} 

can be replaced by

 new_hash = {simon: "Talek", lorem: "Ipsum"} 

Is there a shorter way to record

 :on => :collection 

Applying the same logic causes an error:

 on: :collection 

Refresh to provide more information:

In my .rb routes:

 get 'detail', { on: :member } 

does not work. Also

 get 'detail', on: :member 

Error:

 Exiting SyntaxError: C:/Workspace/OE_11/CyberTrack_Rails3/config/routes.rb:23: syntax error, unexpected ':' get 'detail', { on: :member } 

or

 Exiting SyntaxError: C:/Workspace/OE_11/CyberTrack_Rails3/config/routes.rb:23: syntax error, unexpected ':' get 'detail', on: :member 
+4
source share
3 answers

This works fine for me:

 def get(*args) p *args end get 'detail', on: :member # "detail" # { :on => :member } RUBY_ENGINE # => jruby JRUBY_VERSION # => 1.6.6 

EDIT: Now that you have provided the error message, it looks like you are not using Ruby 1.9. The new hash syntax was introduced in Ruby 1.9; it does not work in older versions. You need to make sure that you are using Ruby 1.9, either by checking that you are using the correct Ruby implementation (for example, YARV supports 1.9, MRI does not work), or if you are using a Ruby implementation that supports several language versions (for example, JRuby), which you pass the correct command line flags (e.g. jruby --1.9 ).

+3
source

As already mentioned, you need to use 1.9 to make this syntax work. Setting the JRUBY_OPTS environment JRUBY_OPTS to --1.9 ensures that you are using the correct ruby ​​version.

+1
source

Bare action: 'edit' and :action => 'edit' are syntax errors. You can pass hashes without parentheses as the last argument to the method. In all other situations, use {action: 'edit'} .

To do this, you need to run ruby ​​1.9+: get 'detail', { on: :member }

-one
source

Source: https://habr.com/ru/post/1399404/


All Articles