Module: Kernel

Defined in:
lib/pattern_matching/functions.rb

Class Method Summary collapse

Class Method Details

.delta(v1, v2) {|item| ... } ⇒ Float

Compute the difference (delta) between two values.

When a block is given the block will be applied to both arguments. Using a block in this way allows computation against a specific field in a data set of hashes or objects.

Parameters:

  • v1 (Object)

    the first value

  • v2 (Object)

    the second value

Yields:

  • iterates over each element in the data set

Yield Parameters:

  • item

    each element in the data set

Returns:

  • (Float)

    positive value representing the difference between the two parameters



53
54
55
56
57
58
59
# File 'lib/pattern_matching/functions.rb', line 53

def delta(v1, v2)
  if block_given?
    v1 = yield(v1)
    v2 = yield(v2)
  end
  return (v1 - v2).abs
end

.pp_s(*objs) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/pattern_matching/functions.rb', line 29

def pp_s(*objs)
  s = StringIO.new
  objs.each {|obj|
    PP.pp(obj, s)
  }
  s.rewind
  s.read
end

.repl?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/pattern_matching/functions.rb', line 11

def repl?
  return ($0 == 'irb' || $0 == 'pry' || $0 == 'script/rails' || !!($0 =~ /bin\/bundle$/))
end

.safe(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/pattern_matching/functions.rb', line 16

def safe(*args, &block)
  raise ArgumentError.new('no block given') unless block_given?
  result = nil
  t = Thread.new do
    $SAFE = 3
    result = self.instance_exec(*args, &block)
  end
  t.join
  return result
end