What it do?

jalcine Aug 6, 2012

For those favoring YAML outputs, this methods simply and recursively outputs the keys and values in YAML (into a String) for your pleasure.

a misprint?

vad4msiu Aug 3, 2012

In section 'Bi-directional associations' an example:

d = Dungeon.first

t = d.traps.first

d.level == t.dungeon.level # => true

d.level = 10

d.level == t.dungeon.level # => false

Then use has_many associations, but lower than written 'for belongs_to associations has_many inverse associations are...

rest of code is in Object#try

roryokane Jul 24, 2012 1 thank

The logic for #try is shared between this method and Object#try – “Show source” here doesn’t show the whole story. Both methods are currently implemented in the file activesupport/lib/active_support/core_ext/object/try.rb .

rest of code is in NilClass#try

roryokane Jul 24, 2012 3 thanks

If you click “Show source” here, you may get confused. The logic for #try is shared between this method and NilClass#try . Both versions are currently implemented in the file activesupport/lib/active_support/core_ext/object/try.rb .

Long-wanted functional extension

EdvardM Jul 23, 2012 2 thanks

This is pretty nice method allowing you to build stuff in a functional way.

Lets say you want to build a hash from an array, keyed by array object, where each value is the number of same objects in the array.

# imperative style :-P

h = Hash.new(0)
[1, 3, 2, 3, 1, 3].each { |i| h[i]...

Replacement

kamilio Jul 23, 2012

==== Replacement

# Method using returning can replaced
def foo
returning Hash.new do |h|
  h[:foo] = "bar"
end
end

# By method using tap  
def foo
Hash.new.tap do |h|
  h[:foo] = "bar"
end
end