Skip to content

Instantly share code, notes, and snippets.

@shu0115
Created February 9, 2015 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shu0115/46b26c67bc1956fc7092 to your computer and use it in GitHub Desktop.
Save shu0115/46b26c67bc1956fc7092 to your computer and use it in GitHub Desktop.
Railsで開発している時によく使う/たまに使うメソッド集 ref: http://qiita.com/shu_0115/items/cc2357441fece13bad5e
nil.aaa
# => NoMethodError: undefined method `aaa' for nil:NilClass
nil.try(:aaa)
# => nil
User.last.attributes.class
# => Hash
User.last.attributes.to_json.class
# => String
JSON.parse(User.last.attributes.to_json).class
# => Hash
User.last.to_json.class
# => String
Digest::SHA1.hexdigest('test')
# => "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
Digest::SHA2.hexdigest('test')
# => "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
Digest::SHA256.hexdigest('test')
# => "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
Digest::SHA384.hexdigest('test')
# => "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9"
Digest::SHA512.hexdigest('test')
# => "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"
Plan.last.days.pluck(:id)
# => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475]
Plan.last.day_ids
# => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475]
{ "a" => "b" }.symbolize_keys
# => {:a=>"b"}
{ 'person' => { 'name' => 'Rob', 'age' => 28 } }.deep_symbolize_keys
# => {:person=>{:name=>"Rob", :age=>28}}
{ a: "b" }.stringify_keys
# => {"a"=>"b"}
Person.find_each do |person|
person.do_awesome_stuff
end
Person.where("age > 21").find_each do |person|
person.party_all_night!
end
Person.where("age > 21").find_in_batches do |group|
sleep(50) # Make sure it doesn't get too crowded in there!
group.each { |person| person.party_all_night! }
end
nil.blank?
# => true
''.blank?
# => true
[].blank?
# => true
{}.blank?
# => true
people.index_by(&:login)
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
nil.present?
# => false
''.present?
# => false
[].present?
# => false
{}.present?
# => false
nil.presence
# => nil
''.presence
# => nil
[].presence
# => nil
{}.presence
# => nil
'a'.presence
# => "a"
1.presence
# => 1
SecureRandom.uuid
# => "c73273d4-68cb-42cd-b454-7478e9af4734"
SecureRandom.hex(16)
# => "3f4a73bd984ccf7947f66109674b1e96"
SecureRandom.hex(8)
# => "10b7662204013166"
'static_pages'.classify
# => "StaticPage"
'StaticPage'.underscore
# => "static_page"
{ a: 1, b: 2, c: 3 }.slice(:a, :c)
# => {:a=>1, :c=>3}
{ a: 1, b: 2, c: 3 }.except(:a, :c)
# => {:b=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment