module Kernel
Public Class Methods
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.
@yield iterates over each element in the data set @yieldparam item each element in the data set
@param [Object] v1 the first value @param [Object] v2 the second value
@return [Float] positive value representing the difference
between the two parameters
# 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
rhaseventh.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html
# 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
# File lib/pattern_matching/functions.rb, line 11 def repl? return ($0 == 'irb' || $0 == 'pry' || $0 == 'script/rails' || !!($0 =~ /bin\/bundle$/)) end
# 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
Private Instance Methods
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.
@yield iterates over each element in the data set @yieldparam item each element in the data set
@param [Object] v1 the first value @param [Object] v2 the second value
@return [Float] positive value representing the difference
between the two parameters
# 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
rhaseventh.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html
# 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
# File lib/pattern_matching/functions.rb, line 11 def repl? return ($0 == 'irb' || $0 == 'pry' || $0 == 'script/rails' || !!($0 =~ /bin\/bundle$/)) end
# 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