浏览 5034 次
|
该帖已经被评为良好帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-01-08
1 stringify_keys and symbolize_keys
>> { 'days' => 25, 'spirit' => 'giving', 'wallet' => 'empty' }.symbolize_keys
=> {:wallet=>"empty", :spirit=>"giving", :days=>25}
>> { 'system' => 'wii', :valid_ages => 5..90 }.stringify_keys
=> {"valid_ages"=>5..90, "system"=>"wii"}
2 assert_valid_keys 方法判断参数的key值是否正确。 User.find(:first, :with => {:active => 1}) find没有:with key值。assert_valid_keys就能派上用处。 >> about_me = { :height => 71, :weight => 160, :likes => 'monster trucks'
=> {:height=>71, :likes=>"monster trucks", :weight=>160}
>> about_me.assert_valid_keys(:height, :weight, :age)
ArgumentError: Unknown key(s): likes
3 diff 返回Hash改变或删除的部分 >> name = { :first => 'Chris', :last => 'Wanstrath', :middle => 'Jebediah' }
=> {:first=>"Chris", :last=>"Wanstrath", :middle=>"Jebediah"}
>> name.diff(:first => 'Chris', :last => 'Wanstrath', :middle => 'Jonesy')
=> {:middle=>"Jebediah"}
>> name.diff(:first => 'Chris', :last => 'Wanstrath')
=> {:middle=>"Jebediah"}
4 from_xml 把xml形式的字符串转换成hash对象 >> Hash.from_xml '<posts><post><id>1</id></post></posts>'
=> {"posts"=>{"post"=>{"id"=>"1"}}}
>> Hash.from_xml '<posts><post><id>1</id></post><post><id>2</id></post></posts>'
=> {"posts"=>{"post"=>[{"id"=>"1"}, {"id"=>"2"}]}}
5 bytes, kilobytes, megabytes, gigabytes, terabytes, petabytes, and exabytes >> 100.bytes => 100 >> 5.kilobytes => 5120 >> 10.megabytes => 10485760 >> 100.gigabytes => 107374182400 >> 2.terabytes => 2199023255552 >> 1.petabytes => 1125899906842624 >> 2.exabytes => 2305843009213693952 6 seconds, minutes, hours, days, weeks, fortnights, months, years, ago / until, since / from_now >> 15.seconds
=> 15
>> 2.minutes
=> 120
>> 30.hours
=> 108000
>> 1.days
=> 86400
>> 2.weeks
=> 1209600
>> 4.fortnights
=> 4838400
>> 2.months
=> 5184000
>> 17.years
=> 536479200
>> 2.days.ago
=> Sat Dec 16 00:34:49 -0800 2006
>> 2.days.ago(Time.now - 3.days)
=> Wed Dec 13 00:34:55 -0800 2006
>> 4.weeks.since("1985-03-13".to_time)
=> Wed Apr 10 00:00:00 UTC 1985
7 ordinalize >> 5.ordinalize => "5th" 8 even? 和 odd?判断是奇数还是偶数(multple_of?) >> 2.even? => true >> 2.odd? => false >> 99.multiple_of? 60 => false >> 25.multiple_of? 5 => true 9 group_by 有点类似sql里的group_by >> magical_people = [ { :name => "Santa Claus", :color => "Red" },
{ :name => "Mrs Claus", :color => "Red" },
{ :name => "Twinkle the Elf", :color => "Green "} ]
=> [{:name=>"Santa Claus", :color=>"Red"}, {:name=>"Mrs Claus", :color=>"Red"}, {:name=>"Twinkle the Elf", :color=>"green "}]
>> magical_people.group_by { |person| person[:color] }
=> {"Green "=>[{:name=>"Twinkle the Elf", :color=>"green "}],
"Red"=>[{:name=>"Santa Claus", :color=>"Red"}, {:name=>"Mrs Claus", :color=>"Red"}]}
10 index_by 可以与上面方法对应。此方法是根据某一个key进行排序 >> beatles = [{ :first => 'John', :last => 'Lennon' },
{ :first => 'Paul', :last => 'McCartney' },
{ :first => 'Evan', :last => 'Weaver' },
{ :first => 'Ringo', :last => 'Starr' }]
=> [{:first=>"John", :last=>"Lennon"}, {:first=>"Paul", :last=>"McCartney"}, {:first=>"Evan", :last=>"Weaver"}, {:first=>"Ringo", :last=>"Starr"}]
>> beatles.index_by { |beatle| beatle[:first] }
=> {"Evan"=>{:first=>"Evan", :last=>"Weaver"},
"Paul"=>{:first=>"Paul", :last=>"McCartney"},
"John"=>{:first=>"John", :last=>"Lennon"},
"Ringo"=>{:first=>"Ringo", :last=>"Starr"}}
11 sum >> [1,2,3,4,5].sum
=> 15
>> Recipe.find(:all).sum { |recipe| recipe.total_time.to_i }
=> 1777
12 alias_attribute 。为某一字段定义一个别名 class User < ActiveRecord::Base alias_attribute :user_id, :id end >> user = User.find(:first) => #<User:0x12622fc ... > >> user.id => 1 >> user.user_id => 1 >> user.user_id? => true 13 attr_accessor_with_default方法重写了attr_accessor方法。在定义时给属性一个默认值。 class Homework attr_accessor_with_default :sucks, true end >> assignment = Homework.new => #<Homework:0x2082f94> >> assignment.sucks => true 14 class_inheritable_* 方法 只与类变量结合使用。class_inheritable_*定义的类变量不在父类和子类之间share。子类改变不会影响到父类。 class_inheritable_reader, class_inheritable_writer, class_inheritable_array_writer, class_inheritable_hash_writer, class_inheritable_accessor, class_inheritable_array, class_inheritable_hash
class Momma
class_inheritable_hash :looks
self.looks = { :hair => 'blonde', :eyes => 'blue' }
end
class Kid < Momma
end
=> {:hair=>"blonde", :eyes=>"blue"}
>> Momma.looks
=> {:hair=>"blonde", :eyes=>"blue"}
>> Kid.looks
=> {:hair=>"blonde", :eyes=>"blue"}
>> Kid.looks.update :eyes => "brown"
=> {:hair=>"blonde", :eyes=>"brown"}
>> Kid.looks
=> {:hair=>"blonde", :eyes=>"brown"}
>> Momma.looks
=> {:hair=>"blonde", :eyes=>"blue"}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-01-20
melin 写道 4 from_xml 把xml形式的字符串转换成hash对象 >> Hash.from_xml '<posts><post><id>1</id></post></posts>'
=> {"posts"=>{"post"=>{"id"=>"1"}}}
>> Hash.from_xml '<posts><post><id>1</id></post><post><id>2</id></post></posts>'
=> {"posts"=>{"post"=>[{"id"=>"1"}, {"id"=>"2"}]}}
这个不错。 |
|
| 返回顶楼 | |




