Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/open_classes/object/guard.rb,
lib/open_classes/object/any_of.rb,
lib/open_classes/object/boolean.rb,
lib/open_classes/object/to_bool.rb,
lib/open_classes/object/my_methods.rb
Overview
Object
Instance Method Summary collapse
-
#any_of?(*args) ⇒ Boolean
If self match any one of args, return true.
-
#boolean? ⇒ Boolean
Check boolean type.
-
#guard(condition) ⇒ Object
guard condition.
-
#my_methods ⇒ Object
Get self define methods.
-
#to_bool ⇒ Object
you get bool value.
-
#unless_guard(condition) ⇒ Object
unless_guard condition.
Instance Method Details
#any_of?(*args) ⇒ Boolean
If self match any one of args, return true.
"hoge".any_of? %w{hoge hige} # => true
"hige".any_of? %w{hoge hige} # => true
"hege".any_of? %w{hoge hige} # => false
10 11 12 13 |
# File 'lib/open_classes/object/any_of.rb', line 10 def any_of?(*args) args.each { |value|return true if self == value } false end |
#boolean? ⇒ Boolean
Check boolean type
boolean? true # => true
boolean? false # => true
boolean? nil # => false
boolean? 'true' # => false
boolean? 'false' # => false
boolean? '' # => false
13 14 15 |
# File 'lib/open_classes/object/boolean.rb', line 13 def boolean? self.is_a?(TrueClass) || self.is_a?(FalseClass) end |
#guard(condition) ⇒ Object
guard condition
Param
-
:condition- guard condition
Example
guard return case
def hoge(msg)
guard(msg) {return "guard"}
"not guard"
end
hoge true # => "guard"
hoge false # => "not guard"
guard fail case
def hoge(msg)
guard(msg) {fail ArgumentError, 'error!!'}
"not guard"
end
hoge true # => raise ArgumentError. message = error!!
hoge false # => "not guard"
32 33 34 |
# File 'lib/open_classes/object/guard.rb', line 32 def guard(condition) yield if condition end |
#my_methods ⇒ Object
Get self define methods.
class SampleClass < String
def public_hello
"public hello"
end
protected
def protected_hello
"protected hello"
end
private
def private_hello
"private hello"
end
end
SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
26 27 28 |
# File 'lib/open_classes/object/my_methods.rb', line 26 def my_methods public_methods(false) + protected_methods(false) + private_methods(false) end |
#to_bool ⇒ Object
you get bool value
true.to_bool # => true
false.to_bool # => false
0.to_bool # => true
1.to_bool # => true
''.to_bool # => true
'true'.to_bool # => true
'false'.to_bool # => true
nil.to_bool # => false
15 16 17 |
# File 'lib/open_classes/object/to_bool.rb', line 15 def to_bool !!self end |
#unless_guard(condition) ⇒ Object
unless_guard condition
Param
-
:condition- guard condition
Example
unless_guard return case
def hoge(msg)
unless_guard(msg) {return "unless_guard"}
"not unless_guard"
end
hoge false # => "unless_guard"
hoge true # => "not unless_guard"
unless_guard fail case
def hoge(msg)
unless_guard(msg) {fail ArgumentError, 'error!!'}
"not unless_guard"
end
hoge false # => raise ArgumentError. message = error!!
hoge true # => "not unless_guard"
63 64 65 |
# File 'lib/open_classes/object/guard.rb', line 63 def unless_guard(condition) yield unless condition end |