Module: Kernel

Defined in:
lib/mug/not.rb,
lib/mug/main.rb,
lib/mug/loop-with.rb

Instance Method Summary collapse

Instance Method Details

#__main__Object

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.



15
16
17
18
19
20
# 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) ⇒ Object

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



8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
# 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, &b) ⇒ Object

Negate a predicate.



6
7
8
# File 'lib/mug/not.rb', line 6

def not(*a, &b)
  not a.empty? ? (b ? (yield self) : self) : __send__(*a, &b)
end