module Kernel

Public Instance Methods

__main__() { || ... } click to toggle source

Compares the calling source filename with `$PROGRAM_NAME` (`$0`).

Returns a falsey value if the calling context is not in the 'main' file.

If called without a block, and the calling context is in the 'main' file, returns true.

If called with a block, and the calling context is in the 'main' file, the block is executed and the result is returned.

# File lib/mug/main.rb, line 15
def __main__
  cloc = caller_locations(1, 1)[0]
  return if cloc.nil?
  return unless File.absolute_path($0) == cloc.absolute_path
  block_given? ? (yield) : true
end
loop_with_index(offset=0) { |c| ... } click to toggle source

Repeatedly executes the block, yielding the current iteration count, which starts from offset. If no block is given, returns an Enumerator.

# File lib/mug/loop-with.rb, line 8
def loop_with_index(offset=0)
  return enum_for(:loop_with_index, offset) unless block_given?
  c = 0 + offset
  begin
    while true
      yield c
      c += 1
    end
  rescue StopIteration
  end
  c
end
loop_with_object(obj) { |obj| ... } click to toggle source

Repeatedly executes the block, yielding an arbitrary object, obj.

# File lib/mug/loop-with.rb, line 24
def loop_with_object(obj)
  return obj=enum_for(:loop_with_object, obj) unless block_given?
  begin
    while true
      yield obj
    end
  rescue StopIteration
  end
  obj
end
not(*a) { |self) : self) : __send__(*a, &b)| ... } click to toggle source

Negate a predicate.

# File lib/mug/not.rb, line 6
def not(*a, &b)
  not a.empty? ? (b ? (yield self) : self) : __send__(*a, &b)
end

Private Instance Methods

with(*args) { |*args| ... } click to toggle source

Yields the arguments to a block.

# File lib/mug/with.rb, line 6
def with *args
  return enum_for(:with, *args) unless block_given?
  yield(*args)
end