Friday, August 24, 2007

Dave Thomas: Rails Treasures Notes

with_scope:

merges conditions
can apply differently to :find and :create clauses
:readonly
cool thing about using hashes as if they were keyword parameters is that keyword parameters become mergeable - a benefit of a hack around a limitation in the language
with_scope being deprecated by core, made private to models

with_options:

merges options, makes it less repetitive
any Ruby object can do this - they put it on Object!
(madness! but cool)
either adds a hash as last parameter or adds values into an existing hash parameter - not necessarily one named options, just whichever one's last in the args.

returning:

awesome!
factors out megadooky "temp var with temp var on last line" pattern

&:xx -> The Blockinator
(aka Symbol#to_proc)

Lookup Constants for caching

now.to_s(:db) # mysql format
now.to_s(:short)
now.to_s(:long)
now.to_s(:rfc822)

merge new strftime strings into the conversions array and you can do
now.to_s(:any_name_you_like)

Range#to_s also extended, (some..range).to_s(:db) becomes BETWEEN clause.

Enumerable#sum
Enumerable#index_by
Enumerable#group_by

Array#in_groups_of

script/generate model wombat --svn

Automatically adds new models to Subversion.

Generic to all generators, so if you write your own, you get it for free.

In console, ActionController::Routing::Routes, no, just irb object and then object is the default object for your entire subsession.

TextMate: Footnotes

2 comments:

  1. Skip returning and use tap instead:
    http://facets.rubyforge.org/rdoc/core/classes/Kernel.html#M000365

    It does much of the same, but calling the method on the object, and will make it to Ruby 1.9.

    ReplyDelete
  2. To be fair, returning is a more intuitive method name. But I'd imagine either one is good.

    ReplyDelete

Note: Only a member of this blog may post a comment.