タグ

2012年2月17日のブックマーク (2件)

  • Module: Enumerable (Ruby 1.9.3)

    The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collec

    an-ironic-man
    an-ironic-man 2012/02/17
    Enumerable#detect/findで、条件に合う最初のオブジェクトを返す。今までselect{...}.firstとかしてたよorz
  • ActiveRecordの属性に「?」で問い合わせる

    ActiveRecordオブジェクトは, class Employee < ActiveRecord::Base end とするだけで,上記で言えばemployees表が持つ各種列に対応する属性が利用可能になる。例えば,employees表にname列が定義されていたとすれば, employee.name = ‘Yoichiro’ p employee.name というように,nameアクセッサを利用することができる。とても簡単だ。では, employee.name? とemployeeオブジェクトにメッセージを投げることが可能である人は,どれだけいるだろうか?もちろん僕も,rails-mlに「 [rails:2302] ActiveRecordオブジェクトの属性にクエスチョンをつけると・・・?」という投稿があるまで,知らなかった。 では,「#{attr_name}?」で問い合わせた場合,

    an-ironic-man
    an-ironic-man 2012/02/17
    active_record_inobject.attr?の仕組み。0や'f'でfalseが返るところは他の言語みたい。