lib/rext/hash/helpers.rb in visionmedia-rext-0.0.6 vs lib/rext/hash/helpers.rb in visionmedia-rext-0.0.7

- old
+ new

@@ -18,6 +18,25 @@ def delete_at *keys keys.map { |key| delete key } end alias :extract! :delete_at + ## + # Return an array of switches and their arguments. + # + # === Examples + # + # { :use_foobar => true }.switchify # => ['--use-foobar'] + # { :use_foobar => false }.switchify # => [] + # { :interval => 15, :icon => :jpeg } # => ['--interval', '15', '--icon', 'jpeg'] + # + + def switchify + inject [] do |args, (key, value)| + next args unless value + args << key.to_s.switchify + args << (String === value ? value.inspect : value.to_s) unless value === true + args + end + end + end \ No newline at end of file