ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年1月9日 星期五
Some good tips
http://errtheblog.com/posts/42-rails-rubyisms-advent
>> { '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"}
>> colors = { :foreground => 'red', :background => 'black' }
=> {:background=>"black", :foreground=>"red"}
>> colors.merge(:background => 'green')
=> {:background=>"green", :foreground=>"red"}
# doesn't override our set colors but would add in :background
# if we didn't already have it:
>> colors.reverse_merge(:background => 'green')
=> {:background=>"black", :foreground=>"red"}
>> 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"}
>> Hash.from_xml '
1
'
=> {"posts"=>{"post"=>{"id"=>"1"}}}
>> Hash.from_xml '
1
2
'
=> {"posts"=>{"post"=>[{"id"=>"1"}, {"id"=>"2"}]}}
>> 2.even?
=> true
>> 2.odd?
=> false
>> 99.multiple_of? 60
=> false
>> 25.multiple_of? 5
=> true
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"}]}
>> 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"}}
>> [1,2,3,4,5].sum
=> 15
>> Recipe.find(:all).sum { |recipe| recipe.total_time.to_i }
=> 1777
class User < ActiveRecord::Base
alias_attribute :user_id, :id
end
>> user = User.find(:first)
=> #
>> user.id
=> 1
>> user.user_id
=> 1
>> user.user_id?
=> true
>> (7.days.ago..1.day.ago).to_s(:db)
=> "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'"
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言