Module: Kernel
- Defined in:
- lib/mug/not.rb,
lib/mug/main.rb,
lib/mug/with.rb,
lib/mug/loop-with.rb
Instance Method Summary collapse
-
#__main__ { ... } ⇒ Boolean, ...
Compares the calling source filename with
$PROGRAM_NAME($0). -
#loop_with_index(offset = 0) {|i| ... } ⇒ Enumerator, Integer
Repeatedly executes the block, yielding the current iteration count, which starts from
offset. -
#loop_with_object(obj) {|obj| ... } ⇒ Enumerator, Object
Repeatedly executes the block, yielding an arbitrary object,
obj. -
#not(*a) {|o| ... } ⇒ Boolean
Negate a predicate.
Instance Method Details
#__main__ { ... } ⇒ Boolean, ...
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.
31 32 33 34 35 36 |
# File 'lib/mug/main.rb', line 31 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) {|i| ... } ⇒ Enumerator, Integer
Similar to iterating with (offset..).each (Ruby 2.6+).
Repeatedly executes the block, yielding the current iteration
count, which starts from offset. If no block is given, returns
an Enumerator.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mug/loop-with.rb', line 23 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| ... } ⇒ Enumerator, Object
Repeatedly executes the block, yielding an arbitrary object, obj.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mug/loop-with.rb', line 53 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) {|o| ... } ⇒ Boolean
Negate a predicate.
When called with no arguments and no block, negates this object. When called with arguments, sends the arguments as a method call to this object and negates the result. When called with a block (and no arguments), yields this object to the block and negates the result.
25 26 27 |
# File 'lib/mug/not.rb', line 25 def not(*a, &b) not a.empty? ? (b ? (yield self) : self) : __send__(*a, &b) end |