Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hashrush.rb

Class Method Summary collapse

Class Method Details

.build_from_symbol_variables(binding, *args) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hashrush.rb', line 4

def self.build_from_symbol_variables(binding, *args)
  raise ArgumentError.new("Oops, expected the first argument to be a binding object. Try `Hash.rush(binding, :some_variable_name)`") if binding.class != Binding

  hash = Hash.new
  args = args.flatten if args.respond_to?(:flatten)
  args.each_with_index do |arg, i|
    raise ArgumentError.new("Oops, argument #{i+1} is not a symbol") unless arg.class == Symbol
    # remove leading '@' symbols from variables
    clean_key = arg.to_s.gsub(/^\@+/, '').to_sym
    begin
      if hash[clean_key] == nil
        hash[clean_key] = binding.eval("#{arg.to_s}")# if is_variable?(arg)
      else
        raise ArgumentError.new("Oops, argument collision detected. :@#{clean_key} and :#{clean_key} at the same time will not work")
      end
    rescue NameError
      raise ArgumentError.new("Oops, looks like the given binding does not have a variable '#{clean_key}'.")
    end
  end
  return hash
end

.rush(binding, *args) ⇒ Object



26
27
28
# File 'lib/hashrush.rb', line 26

def self.rush(binding, *args)
  Hash.build_from_symbol_variables(binding, args)
end