Class: Object

Inherits:
BasicObject
Defined in:
lib/ruby_patch/object.rb

Instance Method Summary collapse

Instance Method Details

#__DIR__Object



18
19
20
21
22
# File 'lib/ruby_patch/object.rb', line 18

def __DIR__()
  called_from = parse_caller(caller(1).first)[:file]
  dir = File.dirname(called_from)
  File.expand_path(dir)
end

#__METHOD__Object



24
25
26
# File 'lib/ruby_patch/object.rb', line 24

def __METHOD__()
  parse_caller(caller(1).first)[:method]
end

#check_type(var, *allow) ⇒ Object

Check type of var. allow is an array which contains Symbol, String or Class object which represents allowable classes. allow can be nested array because they are flattened.



36
37
38
39
40
41
42
43
44
# File 'lib/ruby_patch/object.rb', line 36

def check_type(var, *allow)
  if allow.flatten\
      .map{|klass| klass.to_s.to_sym}\
      .include?(var.class.to_s.to_sym)
    var
  else
    raise TypeError, "#{var}:#{var.class} is not one of #{allow.flatten}"
  end
end

#deep_cloneObject

Create a deep clone by using Marshal.



29
30
31
# File 'lib/ruby_patch/object.rb', line 29

def deep_clone()
  Marshal.load(Marshal.dump(self))
end

#parse_caller(backtrace) ⇒ Hash

Returns:

  • (Hash)


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_patch/object.rb', line 5

def parse_caller(backtrace)
  if /\A(.+?):(\d+)(?::in `(.*)')?/ =~ backtrace
    file = $1
    line = $2.to_i
    method = $3
    {
      file: file,
      line: line,
      method: method,
    }
  end
end