class Object

Object

Object

Object

Object

Object

Object

Object

Object

Public Class Methods

grep_method(search, all = true) click to toggle source
# File lib/open_classes/object/grep_method.rb, line 5
def self.grep_method(search, all = true)
  search = search.to_sym unless search.is_a? Regexp
  methods(all).grep search
end

Public Instance Methods

any_of?(*args) click to toggle source

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
# File lib/open_classes/object/any_of.rb, line 10
def any_of?(*args)
  args.include?(self)
end
boolean?() click to toggle source

Check boolean type

boolean? true # => true
boolean? false # => true
boolean? nil # => false
boolean? 'true' # => false
boolean? 'false' # => false
boolean? '' # => false
# File lib/open_classes/object/boolean.rb, line 13
def boolean?
  self.is_a?(TrueClass) || self.is_a?(FalseClass)
end
grep_method(search, all = true) click to toggle source

grep public instance method

Example

target class

 class GrepMethod
   def self.public_method1;end
   def self.public_method2;end
   def self.public_method11;end
   protected
   def self.protected_method1;end
   def self.protected_method2;end
   def self.protected_method11;end
   private
   def self.private_method1;end
   def self.private_method2;end
   def self.private_method11;end
 end

method call

 GrepMethod.new.grep_method :public_method1, false # => [:public_method1]
 GrepMethod.grep_method :public_method1, false # => [:public_method1]
 GrepMethod.new.grep_method /public_method1/, false # => [:public_method1, :public_method11]
 GrepMethod.grep_method /public_method1/, false # => [:public_method1, :public_method11]
 GrepMethod.new.grep_method /public_method3/, false # => []
 GrepMethod.grep_method /public_method3/, false # => []
 GrepMethod.new.grep_method :__send__, true # => [:__send__]
 GrepMethod.grep_method :__send__, true # => [:__send__]
# File lib/open_classes/object/grep_method.rb, line 41
def grep_method(search, all = true)
  self.class.grep_method(search, all)
end
guard(condition) { || ... } click to toggle source

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"
# File lib/open_classes/object/guard.rb, line 32
def guard(condition)
  yield if condition
end
method_nameable?() click to toggle source

object can use method name or not

"string".method_nameable # => true
:symbol.method_nameable # => true
1.method_nameable # => false
# File lib/open_classes/object/method_nameable.rb, line 11
def method_nameable?
  [String, Symbol].include? self.class
end
my_methods() click to toggle source

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]
# File lib/open_classes/object/my_methods.rb, line 26
def my_methods
  public_methods(false) + protected_methods(false) + private_methods(false)
end
null?() click to toggle source
# File lib/open_classes/object/null.rb, line 4
def null?
  nil?
end
to_bool() click to toggle source

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
# File lib/open_classes/object/to_bool.rb, line 15
def to_bool
  !!self # rubocop:disable DoubleNegation
end
unless_guard(condition) { || ... } click to toggle source

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"
# File lib/open_classes/object/guard.rb, line 63
def unless_guard(condition)
  yield unless condition
end