13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyのチートシート / アクティブサポート

Posted at

ブログを更新しました。元の記事はコチラ


Railsで使いまくりActive Supportなので、そんなにまとめがいが無いし、沢山あるのでまとめられないけれど、代表的なモノをとりあえずまとめておく。
blank?やpresent?やtryなんかは省略する。
 


# ---------------- #
# Active Support
# ---------------- #

require 'active_support/all'

fruits = ["apple", "banana", "peer", "orange"]


puts "\n----配列----\n\n"

puts fruits.from(1).inspect
	# indexからの配列を返す
	#-> ["banana", "peer", "orange"]

puts fruits.to(2).inspect
	# indexまでの配列を返す
	#-> ["apple", "banana", "peer"]

puts fruits.in_groups_of(3).inspect
	# n個ずつに分ける / 足りない分はnilで埋める
	#-> [["apple", "banana", "peer"], ["orange", nil, nil]]

puts fruits.split("banana").inspect
	# "banana"で分割する / "banana"は消える
	#-> [["apple"], ["peer", "orange"]]



puts "\n----時間/日付----\n\n"

puts day = DateTime.new(2012, 12, 21, 11, 34, 45)
	#-> 2012-12-21T11:34:45+00:00
puts day.at_beginning_of_day
	#-> 2012-12-21T00:00:00+00:00
puts day.at_end_of_month
	#-> 2012-12-31T23:59:59+00:00
puts day.at_beginning_of_year
	#-> 2012-01-01T00:00:00+00:00


puts "\n----時間を前後----\n\n"

now = Time.now
puts now.advance(years: 20)
puts now.advance(years: -14) #マイナスだと過去
puts now.advance(yaers: 4, months: 2, weeks: 3, days: 1)
puts now.tomorrow
puts now.yesterday


puts "\n----ハッシュ----\n\n"

# ハッシュの差を求める
#-> { gender: "male" } (*diffはRailsでは非推奨)
options = 		{ user: "sawada", lang: "ja" }
new_options =	{ user: "sawada", lang: "ja", gender: "male" }
puts options.diff(new_options)


# ハッシュのキーを文字列にする
#-> {"user"=>"sawada", "lang"=>"ja"}
puts options.stringify_keys


# キーが重複していなければ、そのハッシュだけ統合する
#-> {:lang=>"ja", :user=>"sawada", :gender=>"male"}
options  = { lang: "ja", user: "sawada" }
defaults = { lang: "en", user: "john", gender: "male" }
puts options.reverse_merge(defaults).inspect


# ハッシュから要素を取り除く
#-> {:user=>"sawada"}
puts options.except(:lang).inspect


# ハッシュのキーが引数(:user, :age)に一致しなかったらエラー
begin
	options.assert_valid_keys(:user, :age)
rescue => error
	puts "Error! #{error.message}"
end



puts "\n----地味に便利な関数----\n\n"

# 偶数/奇数
index = [0, 1, 2, 3, 4]
index.each do |i|
	puts "偶数" if i.even?
	puts "奇数" if i.odd?
end

# Inflector
puts "#{1.ordinalize} prize!"		#-> 1st
puts "#{2.ordinalize} prize!"		#-> 2nd
puts "#{22.ordinalize} prize!"	#-> 23rd

puts "user".pluralize + "(複数形に)"
puts "women".singularize + "(単数系に)"	

puts "work abroad".titleize			#-> Work Abroad
puts "blog_title".humanize			#-> Blog title


 
上のコードを実行すると結果がこうなるはずです。
diffはRailsでは非推奨となっています。


$ ruby active_support.rb 

----配列----

["banana", "peer", "orange"]
["apple", "banana", "peer"]
[["apple", "banana", "peer"], ["orange", nil, nil]]
[["apple"], ["peer", "orange"]]

----時間/日付----

2012-12-21T11:34:45+00:00
2012-12-21T00:00:00+00:00
2012-12-31T23:59:59+00:00
2012-01-01T00:00:00+00:00

----時間を前後----

2034-02-10 16:46:56 +0000
2000-02-10 16:46:56 +0000
2014-05-02 16:46:56 +0000
2014-02-11 16:46:56 +0000
2014-02-09 16:46:56 +0000

----ハッシュ----

{:gender=>"male"}
{"user"=>"sawada", "lang"=>"ja"}
{:lang=>"ja", :user=>"sawada", :gender=>"male"}
{:user=>"sawada"}
Error! Unknown key: lang

----地味に便利な関数----

偶数
奇数
偶数
奇数
偶数
1st prize!
2nd prize!
22nd prize!
users(複数形に)
woman(単数系に)
Work Abroad
Blog title

http://workabroad.jp/posts/2058

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?