Class: Object

Inherits:
BasicObject
Includes:
EvalHelper
Defined in:
lib/open_classes/object/guard.rb,
lib/eval_helper_object.rb,
lib/open_classes/object/null.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,
lib/open_classes/object/grep_method.rb,
lib/open_classes/object/method_nameable.rb,
lib/open_classes/object/grep_instance_method.rb

Overview

Object

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EvalHelper

#attr_accessor_init_code, #attr_init_class_code, #each_brace_code, #each_do_code, #each_with_index_brace_code, #each_with_index_do_code, #if_code, #if_code_after, #require_code, #require_relative_code, #set_variable_code, #set_variables_code, #ternary_operator, #times_code, #unless_code, #unless_code_after

Class Method Details

.grep_method(search, all = true) ⇒ Object



5
6
7
8
# 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

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


13
14
15
# 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) ⇒ Object

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__]


41
42
43
# 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) ⇒ 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

#method_nameable?Boolean

object can use method name or not

"string".method_nameable # => true
:symbol.method_nameable # => true
1.method_nameable # => false

Returns:

  • (Boolean)


11
12
13
# File 'lib/open_classes/object/method_nameable.rb', line 11

def method_nameable?
  [String, Symbol].include? self.class
end

#my_methodsObject

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

#null?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/open_classes/object/null.rb', line 4

def null?
  nil?
end

#to_boolObject

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