Class: Object

Inherits:
BasicObject
Defined in:
lib/fir/patches/try.rb,
lib/fir/patches/blank.rb,
lib/fir/patches/instance_variables.rb

Instance Method Summary collapse

Instance Method Details

#blank?true, false

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?

Returns:

  • (true, false)


16
17
18
# File 'lib/fir/patches/blank.rb', line 16

def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

#instance_valuesObject

Returns a hash with string keys that maps instance variable names without “@” to their corresponding values.

class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}


14
15
16
# File 'lib/fir/patches/instance_variables.rb', line 14

def instance_values
  Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }]
end

#instance_variable_namesObject

Returns an array of instance variable names as strings including “@”.

class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_variable_names # => ["@y", "@x"]


27
28
29
# File 'lib/fir/patches/instance_variables.rb', line 27

def instance_variable_names
  instance_variables.map { |var| var.to_s }
end

#presenceObject

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'

Returns:



43
44
45
# File 'lib/fir/patches/blank.rb', line 43

def presence
  self if present?
end

#present?true, false

An object is present if it’s not blank.

Returns:

  • (true, false)


23
24
25
# File 'lib/fir/patches/blank.rb', line 23

def present?
  !blank?
end

#try(*a, &b) ⇒ Object

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.



64
65
66
# File 'lib/fir/patches/try.rb', line 64

def try(*a, &b)
  try!(*a, &b) if a.empty? || respond_to?(a.first)
end

#try!(*a, &b) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fir/patches/try.rb', line 71

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