å人ç¨ã¡ã¢ã â»è¦ã¦ããã人ãå¤ããããªã®ã§è¿½è¨ãã¾ããã ãã®ãã¡ã¾ã追è¨ãã¾ã
ãªãã©ã«
詳ããã¯ããã¥ã¡ã³ãèªã¿ã¾ããã http://docs.ruby-lang.org/ja/2.1.0/doc/spec=2fliteral.html
Stringã®é å
%w(a b) # ["a", "b"] %w|a b| # ["a", "b"] åºåãæåã¯()以å¤ã«ã<>ã¨ã[]ã¨ã||ã§ã
Stringã®é åã§å¼å±é(W大æå)
c="CCC" %W|a b #{c}| # ["a", "b", "CCC"]
Symbolã®é å
%i(a b) # [:a, :b]
Symbolã®é åã§ãå¼å±éã§ãã
c="CCC" %I|a b #{c}| # [:a, :b, :CCC]
æ£è¦è¡¨ç¾
/Ruby/i # 大æåå°æååºå¥ããªã
String
1æå
?a # "a"
æ®éã®å¼å±é
x = "fuga" "hoge #{x}" # "hoge fuga"
sprintfã£ã½ãå¼å±é
x = "fuga" y = 100 "hoge %s" % x # "hoge fuga" "hoge %s %i" % [x, y] # "hoge fuga 100" è¤æ°ããããã§ï¼
Hashãå±éã§ãã
"Hello %{name} !" % {name: "world"} # "Hello world!"
æ¸å¼æå®ãã§ãã
"%{year}-%
Array
ç©éå
[1, 3, 5, 7, 9] & [1, 2, 3, 4, 5] # [1, 3, 5]
headã¨tailã«å解
head, *tail = [1, 2, 3] head # 1 tail # [2, 3]
zip, transpose
[1, 2, 3].zip [4, 5, 6]ã# [[1, 4], [2, 5], [3, 6]] arr1, arr2 = [[1, 4], [2, 5], [3, 6]].transpose arr1 # [1, 2, 3] arr2 # [4, 5, 6]
çµã¿åãã
[1, 2, 3, 4].combination(2).sort_by{|item| [item[0], item[1]]} # combinationã¯é çªä¿è¨¼ããªããã¨ã«ãªã£ã¦ããã®ã§é©å®sort_by
Enumerable
each_slice
(1..7).each_slice(3).to_a # [[1, 2, 3], [4, 5, 6], [7]]