From: "shugo (Shugo Maeda)" Date: 2021-09-15T08:54:36+00:00 Subject: [ruby-core:105263] [Ruby master Feature#14579] Hash value omission Issue #14579 has been updated by shugo (Shugo Maeda). duerst (Martin D�rst) wrote in #note-25: > Ah, so `{if:}` means something close to `{if: local_variable_get(:if)}` and '{self:}` means something close to `{self: local_variable_get(:self)}` (and NOT `{self: self}`). Yes. Technically speaking, `send(:if)` is used instead of local_variable_get if the local variable is not defined. > Not sure we need this, but also not sure it hurts. In the meeting just after RubyKaigi, someone pointed out that `{if:}[:if]` is faster than `binding.local_variable_get(:if)`. ``` excelsior:ruby$ cat bm.rb require "benchmark" Benchmark.bmbm do |b| ->(if:) { b.report("binding.local_variable_get") do 10000.times do binding.local_variable_get(:if) end end b.report("new hash syntax") do 10000.times do {if:}[:if] end end }.call(if: 123) end excelsior:ruby$ ./ruby bm.rb Rehearsal -------------------------------------------------------------- binding.local_variable_get 0.005680 0.000211 0.005891 ( 0.005889) new hash syntax 0.001817 0.000136 0.001953 ( 0.001965) ----------------------------------------------------- total: 0.007844sec user system total real binding.local_variable_get 0.003668 0.000094 0.003762 ( 0.003763) new hash syntax 0.000829 0.000162 0.000991 ( 0.001042) ``` ---------------------------------------- Feature #14579: Hash value omission https://bugs.ruby-lang.org/issues/14579#change-93673 * Author: shugo (Shugo Maeda) * Status: Assigned * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) ---------------------------------------- How about to allow value omission in Hash literals: ```ruby x = 1 y = 2 h = {x:, y:} p h #=> {:x=>1, :y=>2} ``` And in keyword arguments: ```ruby def login(username: ENV["USER"], password:) p(username:, password:) end login(password: "xxx") #=> {:username=>"shugo", :password=>"xxx"} ``` ---Files-------------------------------- hash_value_omission.diff (619 Bytes) -- https://bugs.ruby-lang.org/ Unsubscribe: