Class: Object

Inherits:
BasicObject
Defined in:
lib/mug/bool.rb,
lib/mug/self.rb,
lib/mug/maybe.rb,
lib/mug/and-or.rb,
lib/mug/iterator/for.rb,
lib/mug/fragile-method-chain.rb

Instance Method Summary collapse

Instance Method Details

#_?Boolean

Begins a FragileMethodChain.

Returns:

  • (Boolean)


57
58
59
# File 'lib/mug/fragile-method-chain.rb', line 57

def _?
	FragileMethodChain.new(self)
end

#and(default) ⇒ Object

Returns either obj or default, depending on the falsiness of obj.

If a block is given, obj is yielded to it; if it returns truthy, default is returned, otherwise obj is returned.



10
11
12
13
14
15
16
# File 'lib/mug/and-or.rb', line 10

def and default
	if block_given?
		yield(self) ? default : self
	else
		self && default
	end
end

#iter_for(meth, *args) ⇒ Object Also known as: to_iter

Creates a new Iterator for the method named meth



8
9
10
# File 'lib/mug/iterator/for.rb', line 8

def iter_for meth, *args
	Iterator.new self, meth, *args
end

#maybe(&b) ⇒ Object

Do something if this object is truthy.

If a block is given, it is executed in the context of this object, iff this object is neither nil nor false.

If no block is given, returns a MaybeDelegator object.



35
36
37
38
39
40
41
# File 'lib/mug/maybe.rb', line 35

def maybe &b
	if b
		self && instance_eval(&b)
	else
		MaybeDelegator.new(self)
	end
end

#or(default) ⇒ Object

Returns either obj or default, depending on the truthiness of obj.

If a block is given, obj is yielded to it; if it returns truthy, obj is returned, otherwise default is returned.



24
25
26
27
28
29
30
# File 'lib/mug/and-or.rb', line 24

def or default
	if block_given?
		yield(self) ? self : default
	else
		self || default
	end
end

#revapply(*args, &block) ⇒ Object

Yields this object (and any other arguments) to a block. If no block is given, returns an Enumerator.



29
30
31
32
33
34
35
# File 'lib/mug/self.rb', line 29

def revapply(*args, &block)
	if block_given?
		yield self, *args
	else
		enum_for(:revapply, *args) { args.length + 1 }
	end
end

#self(&block) ⇒ Object Also known as: itself

Returns this object.

If a block is given, this object is yielded to it, and the result is returned.



9
10
11
12
13
14
15
# File 'lib/mug/self.rb', line 9

def self(&block)
	if block_given?
		yield self
	else
		self
	end
end

#to_bObject

Converts obj to a boolean.



20
21
22
# File 'lib/mug/bool.rb', line 20

def to_b
	true
end

#to_boolObject

Converts obj to a boolean.



13
14
15
# File 'lib/mug/bool.rb', line 13

def to_bool
	true
end

#yield(&block) ⇒ Object

Deprecated alias for #self



19
20
21
22
# File 'lib/mug/self.rb', line 19

def yield(&block) #:nodoc:
	warn 'Object#yield is deprecated; use Object#self'
	self.self(&block)
end