class Object

Public Instance Methods

deep_send(*args) click to toggle source

Invokes the specified methods continuously, unless encountering a nil value.

Examples

10.deep_send("to_s.length")      # => 2
10.deep_send("to_s", "length")   # => 2
10.deep_send(:to_s, :length)     # => 2
10.deep_send(["to_s", "length"]) # => 2
[].deep_send("first.length")     # => nil
# File core_extensions/object.rb, line 32
def deep_send(*args)
  args = args.first.dup if args.length == 1 && args.first.kind_of?(Array)
  args = args.shift.to_s.strip.split('.') + args

  arg = args.shift
  raise ArgumentError if arg.nil?

  result = send(arg)
  return nil if result.nil?
  return result if args.empty?

  result.deep_send(args)
end
in_namespace?(val) click to toggle source

Returns whether or not the object is in the given namespace.

Arguments

  • val - The object to be checked in the namespace.

Examples

Aaa::Bbb::Ccc::Ddd.in_namespace?(Aaa::Bbb)           #=> true
Aaa::Bbb::Ccc::Ddd.new.in_namespace?(Aaa::Bbb)       #=> true
Aaa::Bbb::Ccc::Eee.in_namespace?("Aaa::Bbb")         #=> true
Aaa::Bbb::Ccc::Eee.in_namespace?(Aaa::Bbb::Ccc::Ddd) #=> false
# File core_extensions/object.rb, line 58
def in_namespace?(val)
  namespaces = val.to_s.split("::")
  namespaces == (kind_of?(Module) ? namespaces : self.class.namespaces)[0, namespaces.length]
end
is_one_of?(*args) click to toggle source

Returns true if the receiver object is an instance of at least one of the classes specified by args. This method is similar to Object#is_a?, but it accepts multiple arguments.

Arguments

  • args - The classes to check against. One or more classes can be passed. Each argument represents a class that the object will be checked against.

Examples

1.is_one_of?(Numeric, TrueClass)     #=> true
true.is_one_of?(Numeric, TrueClass)  #=> true
false.is_one_of?(Numeric, TrueClass) #=> false

Returns

(Boolean)

# File core_extensions/object.rb, line 19
def is_one_of?(*args)
  args.any? { |arg| is_a?(arg) }
end