Doesn't return nil if the object you try from isn't nil.

nhance Jan 6, 2010 6 thanks

Note that this doesn't prevent a NoMethodError if you attempt to call a method that doesn't exist on a valid object.

a = Article.new

a.try(:author) #=> #<Author ...>

nil.try(:doesnt_exist) #=> nil

a.try(:doesnt_exist) #=> NoMethodError: undefined method `doesnt_exist' for #<Artic...

Version Ranges

slippyd Dec 18, 2009 4 thanks

To specify a version range, use array syntax like this: config.gem 'paperclip', :version => ['>= 2.3.1.1', '< 3.0']

The example will, of course, match any version 2.3.1.1 or newer up until (not including) 3.0 or later.

yijisoo

sfusion Dec 13, 2009 1 thank

create generates the object and saves. new only generates the object.

e.g.

o = Object.new(:foo => 'bar')
o.save

is the same as

o = Object.create(:foo => 'bar')

Replacing with "\\" and match

Mange Dec 3, 2009 2 thanks

If you're trying to place a "\" in front of your matches, you'll quickly see that it is a pain in the ass to add the quoting in the replacement string.

Here's an example: v = "Foo Bar!" # Target: Foo\ Bar\! # Resulting strings will not be quoted to decrease # the amount of backslash...

Capturing blocks after >2.2

mechazoidal Dec 3, 2009 1 thank

After 2.2, you can omit the do ...end block and simply use the &block variable directly: concat(content_tag(:div, :class => "wrapped_content") do capture(&block) end, block.binding)

becomes simply:

concat(content_tag(:div, capture(&block), :class => "wrapped_content"))