Console Syntax for Ruby Hashes

Do these two statements perform the same type of argument (hash) for the new method?

@seat = Seat.new(:flight_id => @flight.id)

@seat = Seat.new({:flight_id => @flight.id})

Do the hash brackets {} change anything in the second example?

+3
source share
2 answers

They are both the same, {} do not add anything in the second argument, except that they make things even more explicit than they already were (using the syntax => is enough to say "this hash" to anyone who uses ruby ​​for any period of time) .

Ruby will automatically include a list of options such as:

someFunction(:arg1 => value1, :arg2 => value2)

. , {} , - , , (, , html_options), :

someFunction({:arg1 => value1, :arg2 => value2}, {:arg3 => value3})

( , 2 , , {}, , )

: " ".

+12

, , ( ):

@seat = Seat.new({:flight_id, @flight.id})

, workmad3, (= > ) . Ruby (irb) , :

{:eyes, "blue", :height, 6.2}  # => {:height=>6.2, :eyes=>"blue"}

Ruby 1.9, :

{eyes: "blue", height: 6.2}
+1

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


All Articles