Module: SimpleHash
- Defined in:
- lib/simple_hash.rb,
lib/simple_hash/version.rb
Constant Summary collapse
- VERSION =
'0.0.2'
Class Method Summary collapse
-
.h(&list_block) ⇒ Hash
Make a hash from an array of symbols wrapped in a block Suppose you have variables.
-
.short_cut! ⇒ Object
Adds #h method globally, delegating to SimpleHash::h.
Class Method Details
.h(&list_block) ⇒ Hash
Make a hash from an array of symbols wrapped in a block Suppose you have variables
year = 2016
month = 3
day = 30
hour = 18
minute = 50
second = 23
Bulid the hash like this
SimpleHash.h{[:year, :month, :day, :hour, :minute, :second]}
This method gets the keys by yieling the block passed in and look up the variable values thourgh the block’s binding
23 24 25 26 27 28 |
# File 'lib/simple_hash.rb', line 23 def h(&list_block) variables = yield.map(&:to_s) Hash.[](variables.map do |variable| [variable.to_sym, list_block.binding.local_variable_get(variable)] end) end |
.short_cut! ⇒ Object
Adds #h method globally, delegating to SimpleHash::h
31 32 33 |
# File 'lib/simple_hash.rb', line 31 def self.short_cut! TOPLEVEL_BINDING.eval 'include SimpleHash' end |