class Object

github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb

Public Instance Methods

blank?() click to toggle source

An object is blank if it's false, empty, or a whitespace string. For example, '', ' ', nil, [], and {} are all blank.

This simplifies

address.nil? || address.empty?

to

address.blank?

@return [true, false]

# File lib/wedge/utilis/blank.rb, line 18
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end
deep_dup() click to toggle source

Returns a deep copy of object if it's duplicable. If it's not duplicable, returns self.

object = Object.new
dup    = object.deep_dup
dup.instance_variable_set(:@a, 1)

object.instance_variable_defined?(:@a) # => false
dup.instance_variable_defined?(:@a)    # => true
# File lib/wedge/utilis/hash.rb, line 119
def deep_dup
  duplicable? ? dup : self
end
duplicable?() click to toggle source

Can you safely dup this object?

False for nil, false, true, symbol, number objects; true otherwise.

# File lib/wedge/utilis/duplicable.rb, line 24
def duplicable?
  true
end
presence() click to toggle source

Returns the receiver if it's present otherwise returns nil. object.presence is equivalent to

object.present? ? object : nil

For example, something like

state   = params[:state]   if params[:state].present?
country = params[:country] if params[:country].present?
region  = state || country || 'US'

becomes

region = params[:state].presence || params[:country].presence || 'US'

@return [Object]

# File lib/wedge/utilis/blank.rb, line 45
def presence
  self if present?
end
present?() click to toggle source

An object is present if it's not blank.

@return [true, false]

# File lib/wedge/utilis/blank.rb, line 25
def present?
  !blank?
end
require(name) click to toggle source

rewrite require

# File lib/wedge/require.rb, line 3
def require(name)
  Kernel.require name

  return unless defined?(Wedge) && Wedge.respond_to?(:config)

  if name[/\Awedge/] || name[Dir.pwd]
    name       = name.sub("#{Dir.pwd}/", '').gsub(/\.rb$/, '').gsub(/\//, '__')
    caller_str = "#{caller[0]}".gsub(/(#{Dir.pwd}\/|.*(?=wedge))/, '').gsub(/:.+$/, '').gsub(/\.rb$/, '').gsub(/\//, '__')

    if !caller_str['.'] && !(Wedge.config.requires[caller_str] ||= []).include?(name)
      Wedge.config.requires[caller_str] << name
    end
  end
end
require_relative(name) click to toggle source

rewrite require_relative

# File lib/wedge/require.rb, line 19
def require_relative(name)
  caller_str       = "#{caller[0]}".gsub(/:.+$/, '').gsub(/\.rb$/, '')
  caller_path_name = caller_str.gsub(%r{(#{Dir.pwd}/|.*wedge)}, '').gsub(/:.+$/, '').gsub(/^\//, '')

  path_name = caller_path_name.gsub(/(?<=\/)([^\/]*)$/, "#{name}")
  path_name = File.expand_path(path_name).sub("#{Dir.pwd}/", '') if path_name['..']
  path_name = path_name.gsub(/\//, '__')
  file      = caller_str.gsub(/(?<=\/)([^\/]*)$/, "#{name}")
  caller_path_name = caller_path_name.gsub(/\//, '__')

  if !caller_path_name['.'] && !(Wedge.config.requires[caller_path_name] ||= []).include?(path_name)
    Wedge.config.requires[caller_path_name] << path_name
  end

  Kernel.require file
end
try(*a, &b) click to toggle source

Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.

This method is defined to be able to write

@person.try(:name)

instead of

@person.name if @person

try calls can be chained:

@person.try(:spouse).try(:name)

instead of

@person.spouse.name if @person && @person.spouse

try will also return nil if the receiver does not respond to the method:

@person.try(:non_existing_method) # => nil

instead of

@person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil

try returns nil when called on nil regardless of whether it responds to the method:

nil.try(:to_i) # => nil, rather than 0

Arguments and blocks are forwarded to the method if invoked:

@posts.try(:each_slice, 2) do |a, b|
  ...
end

The number of arguments in the signature must match. If the object responds to the method the call is attempted and ArgumentError is still raised in case of argument mismatch.

If try is called without arguments it yields the receiver to a given block unless it is nil:

@person.try do |p|
  ...
end

You can also call try with a block without accepting an argument, and the block will be instance_eval'ed instead:

@person.try { upcase.truncate(50) }

Please also note that try is defined on Object. Therefore, it won't work with instances of classes that do not have Object among their ancestors, like direct subclasses of BasicObject. For example, using try with SimpleDelegator will delegate try to the target instead of calling it on the delegator itself.

# File lib/wedge/utilis/try.rb, line 62
def try(*a, &b)
  try!(*a, &b) if a.empty? || respond_to?(a.first)
end
try!(*a) { |self| ... } click to toggle source

Same as try, but raises a NoMethodError exception if the receiver is not nil and does not implement the tried method.

"a".try!(:upcase) # => "A"
nil.try!(:upcase) # => nil
123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum
# File lib/wedge/utilis/try.rb, line 72
def try!(*a, &b)
  if a.empty? && block_given?
    if b.arity.zero?
      instance_eval(&b)
    else
      yield self
    end
  else
    public_send(*a, &b)
  end
end