Created
February 9, 2015 16:06
-
-
Save shu0115/46b26c67bc1956fc7092 to your computer and use it in GitHub Desktop.
Railsで開発している時によく使う/たまに使うメソッド集 ref: http://qiita.com/shu_0115/items/cc2357441fece13bad5e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nil.aaa | |
# => NoMethodError: undefined method `aaa' for nil:NilClass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nil.try(:aaa) | |
# => nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.last.attributes.class | |
# => Hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.last.attributes.to_json.class | |
# => String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.parse(User.last.attributes.to_json).class | |
# => Hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.last.to_json.class | |
# => String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Plan.last.days.pluck(:id) | |
# => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Plan.last.day_ids | |
# => [13468, 13469, 13470, 13471, 13472, 13473, 13474, 13475] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person.find_each do |person| | |
person.do_awesome_stuff | |
end | |
Person.where("age > 21").find_each do |person| | |
person.party_all_night! | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nil.blank? | |
# => true | |
''.blank? | |
# => true | |
[].blank? | |
# => true | |
{}.blank? | |
# => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ...>, ...} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nil.present? | |
# => false | |
''.present? | |
# => false | |
[].present? | |
# => false | |
{}.present? | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nil.presence | |
# => nil | |
''.presence | |
# => nil | |
[].presence | |
# => nil | |
{}.presence | |
# => nil | |
'a'.presence | |
# => "a" | |
1.presence | |
# => 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SecureRandom.uuid | |
# => "c73273d4-68cb-42cd-b454-7478e9af4734" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SecureRandom.hex(16) | |
# => "3f4a73bd984ccf7947f66109674b1e96" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SecureRandom.hex(8) | |
# => "10b7662204013166" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'static_pages'.classify | |
# => "StaticPage" | |
'StaticPage'.underscore | |
# => "static_page" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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