Class: Object

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

Instance Method Summary collapse

Instance Method Details

#__DIR__Object



21
22
23
24
25
# File 'lib/ruby_patch/object.rb', line 21

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

#__METHOD__Object



27
28
29
# File 'lib/ruby_patch/object.rb', line 27

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.



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_patch/object.rb', line 39

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.



32
33
34
# File 'lib/ruby_patch/object.rb', line 32

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

#exception?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/ruby_patch/object.rb', line 3

def exception?()
  self.class.posterity_of?(Exception)
end

#parse_caller(backtrace) ⇒ Hash

Returns:

  • (Hash)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_patch/object.rb', line 8

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