Top Level Namespace

Defined Under Namespace

Modules: Enumerable, Info, Kernel, OS, RubyEngine, RubyVersion, Zucker Classes: Array, Binding, Class, Dir, File, Hash, Module, NilClass, Object, Regexp, String, Symbol

Constant Summary collapse

Infinity =

constants - who would use these in real-world code for other things?

1.0 / 0.0
NaN =

or 2*Float::MAX or Float::INFINITY

0.0 / 0.0

Instance Method Summary collapse

Instance Method Details

#alias_for(m, *aliases) ⇒ Object Also known as: aliases_for



4
5
6
7
8
# File 'lib/zucker/alias_for.rb', line 4

def alias_for(m, *aliases)
  aliases.each{ |a|
    class_eval "alias #{a} #{m}"
  }
end

#egonil(&block) ⇒ Object Also known as: nn

code by Yohan, slightly edited and comments by me



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zucker/egonil.rb', line 5

def egonil(&block)
  # grip methods
  ori_method_missing   = NilClass.instance_method(:method_missing)
  catch_method_missing = NilClass.instance_method(:catch_method_missing)
  # activate ego mode
  NilClass.send :define_method, :method_missing, catch_method_missing
  # run code
  yield
ensure
  # no matter what happens: restore default nil behaviour
  NilClass.send :define_method, :method_missing, ori_method_missing
end

#instance_variables_from(obj, *only) ⇒ Object Also known as: ivars



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

def instance_variables_from(obj, *only)
  iter =
  if    obj.is_a? Binding
    obj.eval('local_variables').map{|e| [obj.eval("#{e}"), e] }
  elsif obj.is_a? Hash
    obj.map{|k,v| [v,k] }
  else
#  elsif obj.is_a? Enumerable
    obj.each.with_index
  end

  ret = []
  iter.each{ |value, arg|
    arg = arg.to_s
    if only.include?(arg) || only.include?(arg.to_sym) || only.empty?
      arg = '_' + arg  if (48..57).member? arg.unpack('C')[0]  # 1.8+1.9
      ret << ivar = :"@#{arg}"
      self.instance_variable_set ivar, value
    end
  }
  ret
end

#iterate(*params) ⇒ Object

Raises:

  • (ArgumentError)


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

def iterate(*params)
  # params.shift.zip(*params).each{ |*elements| yield *elements }
  raise ArgumentError, "wrong number of arguments (0)" if params.empty?

  first = params.shift
  if params.empty? # single param - like each
    if block_given?
      first.map{|e| yield e }
    else
      first.map.to_enum
    end
  else # multiple params
    max_size = [first, *params].max_by(&:count).size
    padded_first = first.to_a + [nil]*(max_size - first.count)  # append nils
    obj = padded_first.zip(*params)
    if block_given?
      obj.map{|es| yield(*es) }
    else
      obj.map.to_enum
    end
  end
end

#make_new(what, *args, &block) ⇒ Object



8
9
10
# File 'lib/zucker/tap.rb', line 8

def make_new(what, *args, &block)
  what.new(*args).tap(&block)
end

#tap_on(obj, &block) ⇒ Object



4
5
6
# File 'lib/zucker/tap.rb', line 4

def tap_on(obj, &block)
  obj.tap(&block)
end