Module: Hammock::ObjectPatches::InstanceMethods

Defined in:
lib/hammock/monkey_patches/object.rb

Instance Method Summary collapse

Instance Method Details

#in?(first, *rest) ⇒ Boolean

The reverse of Enumerable#include? - returns true if self is equal to one of the elements of args.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/hammock/monkey_patches/object.rb', line 34

def in? first, *rest
  if first.respond_to?(:include?) && rest.empty?
    first.include? self
  else
    [first].concat(rest).include? self
  end
end

#send_if(condition, method_name, *args) ⇒ Object

If condition evaluates to true, return the result of sending method_name to self; *args to self, otherwise, return self as-is.



54
55
56
# File 'lib/hammock/monkey_patches/object.rb', line 54

def send_if condition, method_name, *args
  condition ? send(method_name, *args) : self
end

#send_if_respond_to(method_name, *args) ⇒ Object

If condition evaluates to true, return the result of sending method_name to self; *args to self, otherwise, return self as-is.



59
60
61
# File 'lib/hammock/monkey_patches/object.rb', line 59

def send_if_respond_to method_name, *args
  send_if respond_to?(method_name), method_name, *args
end

#symbolizeObject

A symbolized, underscored (i.e. reverse-camelized) representation of self.

Examples:

Hash.symbolize                     #=> :hash
ActiveRecord::Base.symbolize       #=> :"active_record/base"
"GetThisCamelOffMyCase".symbolize  #=> :get_this_camel_off_my_case


49
50
51
# File 'lib/hammock/monkey_patches/object.rb', line 49

def symbolize
  self.to_s.underscore.to_sym
end

#tap {|_self| ... } ⇒ Object

Return self after yielding to the given block.

Useful for inline logging and diagnostics. Consider the following:

@items.map {|i| process(i) }.join(", ")

With tap, adding intermediate logging is simple:

@items.map {|i| process(i) }.tap {|obj| log obj.inspect }.join(", ")

– TODO Remove for Ruby 1.9

Yields:

  • (_self)

Yield Parameters:



27
28
29
30
# File 'lib/hammock/monkey_patches/object.rb', line 27

def tap
  yield self
  self
end