Module: EmanLib

Defined in:
lib/emanlib.rb,
lib/patch/let.rb,
lib/patch/enum.rb,
lib/patch/lambda.rb

Defined Under Namespace

Modules: Enum Classes: Lambda, Maplet

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.let(*args, &block) ⇒ Object

A convenient shorthand for ‘Maplet.new.define!(…)`.

Examples:

person = let(name: "Rio", age: 37)
puts person.name # => "Rio"
puts person.age  # => 37

point = let **{x: 10, y: 20}
puts point.x # => 10

settings = let do
  theme = "dark"
  font_size = 12
  binding
end
puts settings.theme # => "dark"

complex_data = let([[:id, 42]], name: "Xed") do
  details = { color: "red", size: "large" }
  binding # Required
end

puts complex_data.id            # => 42
puts complex_data.name          # => "Xed"
puts complex_data.details.color # => "red"

Parameters:

  • args (Array<Hash, Array>)

    A list of Hashes or “hashy” Arrays.

  • block (Proc)

    If provided, its local variables are used to define methods.

Returns:

  • (Object)

    A new object with dynamically defined methods.

See Also:

  • [Maplet[Maplet#define!]


116
117
118
# File 'lib/patch/let.rb', line 116

def let(*args, &block)
  Maplet.new.define!(*args, &block)
end

.support_lambdaObject

Support for using a _ as the second operand with operators. WARN: This method WILL MODIFY the standard library classes. In particular, the operators: ‘- * / % ** & | ^ << >> <=> == === != > < >= <=` in the classes: `Integer, Float, Rational, Complex, Array, String, Hash, Range, Set`



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/patch/lambda.rb', line 165

def support_lambda
  [[Integer, Float, Rational, Complex, Array, String, Hash, Range, Set],
   %i[- * / % ** & | ^ << >> <=> == === != > < >= <=]].op(:product)
    .each do |klass, op|
    next unless klass.instance_methods(false).include?(op)

    original = klass.instance_method(op)
    klass.define_method(op) do |other|
      if other.is_a?(Lambda)
        repr = [self]
        repr.concat(other.repr)
        repr << Lambda::Function.new(op)
        Lambda.new(repr)
      else
        original.bind(self).call(other)
      end
    end
  end
end