タグ

ブックマーク / micdonalds.hatenadiary.org (4)

  • instance_of?とis_a?の違い - mic_footprints

    instance_of?とis_a?の違いを、メモ。 以下のものを用いて実験 module Foo; end module Bar; end class Hoge include Foo end class Fuga < Hoge include Bar end fuga = Fuga.new instance_of? fuga.instance_of? Fuga #=> true fuga.instance_of? Hoge #=> false fuga.instance_of? Object #=> false fuga.instance_of? Foo #=> false fuga.instance_of? Bar #=> false selfが、引数で指定されたクラスの直接のインスタンスの場合、trueを返す is_a?メソッド fuga.is_a? Fuga #=> true f

    instance_of?とis_a?の違い - mic_footprints
    suginoy
    suginoy 2014/08/05
  • joins オプションとinclude オプションの決定的な違い - mic_footprints

    railsのfindメソッドのincludeオプションとjoinsオプションの違いに付いてまとめてみた 環境 rails 1.2.3 DB構成 mysql> DESCRIBE users; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | name | varchar(40) | YES | | | | +-------+-------------+------+-----+---------

    joins オプションとinclude オプションの決定的な違い - mic_footprints
  • sortとsort_by - mic_footprints

    今回は、Rubyに関して書きます sortメソッド Arrayクラスのメソッド 配列内の要素を並び替えすためのメソッド 数の大きさ 行の長さ アルファベット順 アルファベット逆順 などなど たとえば、 array = ["a", "c", "b", "f", "d", "e"] sorted = array.sort do |a, b| a <=> b end p sorted #=> ["a", "b", "c", "d", "e", "f"] sortメソッドは、配列の要素を余分に呼び出します。 どういう事かというと、 array = ["a", "c", "b", "f", "d", "e"] i = 0 sorted = array.sort do |a, b| i += 2 a <=> b end p sorted #=> ["a", "b", "c", "d", "e", "f

    sortとsort_by - mic_footprints
    suginoy
    suginoy 2014/01/30
    “sort_byで安定ソートを実現する”
  • ARのsaveとかupdate_attributeとか - mic_footprints

    ActiveRecordのsaveやupdate_attribute(s)がどのような仕組みになっているのかのメモ 環境 rails1.2.3 ソースコード # activerecord/lib/active_record/base.rb 1545 def save 1546 create_or_update 1547 end # 中略 1584 def update_attribute(name, value) 1585 send(name.to_s + '=', value) 1586 save 1587 end # 中略 1591 def update_attributes(attributes) 1592 self.attributes = attributes 1593 save 1594 end # 中略 1787 private 1788 def create_or_upda

    ARのsaveとかupdate_attributeとか - mic_footprints
    suginoy
    suginoy 2014/01/27
    "update_attributeメソッドの場合、save(false)として呼び出しており、validationを通らないので、誤った情報がDBに書き加えられることになる可能性があるので、要注意"
  • 1