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

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.

Examples:

Without a block

require 'mug/main'
if __main__
  puts "the main file"
end

With a block

require 'mug/main'
__main__ do
  puts "also the main file"
end

Yields:

  • optional block to execute when in the main file

Returns:

  • (Boolean, Object, nil)

    true if in main (no block), block result (with block), or nil if not in main



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

Note:

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.

Examples:

require 'mug/loop-with'
loop_with_index do |i|
  p i
  break
end

Parameters:

  • offset (Integer) (defaults to: 0)

    the starting count value

Yields:

  • (i)

    yields the current iteration count

Yield Parameters:

  • i (Integer)

    the current count

Returns:

  • (Enumerator)

    if no block is given

  • (Integer)

    the final count value when the loop terminates



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.

Examples:

require 'mug/loop-with'
arr = loop_with_object([]) do |a|
  s = gets.chomp
  throw StopIteration if s.empty?
  a << s
end

Parameters:

  • obj (Object)

    the object to yield on each iteration

Yields:

  • (obj)

    yields the object on each iteration

Yield Parameters:

  • obj (Object)

    the arbitrary object

Returns:

  • (Enumerator)

    if no block is given

  • (Object)

    the yielded object when the loop terminates



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.

Examples:

require 'mug/not'
false.not              #=> true
true.not               #=> false
[].not(:empty?)        #=> false
[1].not(:empty?)       #=> true
[1,-2,3].not(:all?) {|e| e > 0 }  #=> true

Parameters:

  • a (Array)

    arguments to send as a method call

Yields:

  • (o)

    optionally yields self to the block

Yield Parameters:

Returns:

  • (Boolean)

    the negated 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