Module: Hammock::ObjectPatches::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#in?(*args) ⇒ Boolean

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/hammock/monkey_patches/object.rb', line 34

def in? *args
  args.include? self
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.



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

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.



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

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


45
46
47
# File 'lib/hammock/monkey_patches/object.rb', line 45

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