Using Unshift with Load Path's

WedTM Feb 11, 2010 1 thank

Using unshift on your load path's for Rails, Sinatra, etc, is a good way of ensuring that the file your including is going to be first.

== Example vendor = File.join(File.dirname(FILE), 'vendor') $LOAD_PATH.unshift File.expand_path(File.join(vendor, 'ultraviolet-0.10.5', 'lib'...

Odd Number of padding characters

WedTM Feb 11, 2010

In the case of an odd number of empty spaces in a length, Ruby will append the extra character to the right-hand side of the string.

Example:

irb(main):002:0> "Hello".center(10,"-")
=> "--Hello---"

Paying attention to query parameters

chrisrbailey Jan 29, 2010 3 thanks

Standard action caching ignores query parameters, which means you'd get the same results for a URL with and without query parameters if it was action cached. You can make it pay attention to them by using a custom cache path like so:

caches_action :my_action, :cache_path => Proc.new { |c| c.pa...

File name without the extension

metavida Jan 27, 2010 1 thank

To get the file name without the extension you can use File.extname in combination with File.basename

File.basename("test.rb", File.extname("test.rb"))             #=> "test"
File.basename("a/b/d/test.rb", File.extname("a/b/d/test.rb")) #=> "test"
File.basename("test", File.extname("tes...

returns an ActiveSupport::OrderedHash

ColinDKelley Jan 25, 2010 2 thanks

Returns an ActiveSupport::OrderedHash, which is a subclass of Hash that preserves order. If you're running Ruby 1.9, it is simply an alias for Hash. Surprisingly, you might not realize that OrderedHash is preserving order since it delegates its inspect method to Hash. More at ActiveSupport::Ordere...

custom uniq method

bvida Jan 25, 2010 2 thanks

Build hash from elements of your Array using attribute as key and the element as value and return values of Hash:

Hash[*ary.map {|obj| [obj.name, obj]}.flatten].values