class Object
Public Instance Methods
An object is blank if it's false, empty, or a whitespace string. For example, nil
, '', ' ', [], {}, and false
are all blank.
This simplifies
!address || address.empty?
to
address.blank?
@return [true, false]
# File lib/simple_ext/object/blank.rb, line 17 def blank? respond_to?(:empty?) ? !!empty? : !self end
Returns true if this object is included in the argument. Argument must be any object which responds to #include?
. Usage:
characters = ["Konata", "Kagami", "Tsukasa"] "Konata".in?(characters) # => true
This will throw an ArgumentError
if the argument doesn't respond to #include?
.
# File lib/simple_ext/object/inclusion.rb, line 12 def in?(another_object) another_object.include?(self) rescue NoMethodError raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end
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}
# File lib/simple_ext/object/instance_variables.rb, line 14 def instance_values Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }] end
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"]
# File lib/simple_ext/object/instance_variables.rb, line 27 def instance_variable_names instance_variables.map(&:to_s) end
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/simple_ext/object/blank.rb, line 44 def presence self if present? end
Returns the receiver if it's included in the argument otherwise returns nil
. Argument must be any object which responds to #include?
. Usage:
params[:bucket_type].presence_in %w( project calendar )
This will throw an ArgumentError
if the argument doesn't respond to #include?
.
@return [Object]
# File lib/simple_ext/object/inclusion.rb, line 26 def presence_in(another_object) in?(another_object) ? self : nil end
An object is present if it's not blank.
@return [true, false]
# File lib/simple_ext/object/blank.rb, line 24 def present? !blank? end
Alias of to_s
.
# File lib/simple_ext/object/to_query.rb, line 7 def to_param to_s end
Converts an object into a string suitable for use as a URL query string, using the given key
as the param name.
# File lib/simple_ext/object/to_query.rb, line 13 def to_query(key) "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}" end
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
.
# File lib/simple_ext/object/try.rb, line 5
# File lib/simple_ext/object/try.rb, line 93 def try!(method_name = nil, *args, &b) if method_name.nil? && block_given? if b.arity == 0 instance_eval(&b) else yield self end else public_send(method_name, *args, &b) end end