åå¿é²å
¼ãåå¿è
åãã®ã¡ã½ããä¸è¦§ã
ä½è£ãããã°Railsç·¨ã¨ããæ¸ããããªã
ããå¿ãã¦ãã¨ãä½äº?ã¨ãããçªã£è¾¼ã¿ãããã°æ¯éãé¡ããã¾ãã
ããã¡ãã£ã¨ã³ã¼ãã®å
·ä½ä¾æ¸ãããã£ããã©ãããã©ããããªã£ã¦ããw
Array
Array#combination(n), Array#permutation(n)
é
åã®ãã¼ã¿ãå
ã«çµã¿åãã(é å)ãä½ã£ã¦ããã¾ãã
ãããã¯ã渡ããªãå ´åã¯Enumeratorã§çµã¿åããã®åè¦ç´ ãåæãã¾ãã
Enumerator
Enumerator#with_index(offset = 0)
Enumeratorã®åè¦ç´ ã«åããã¦indexãåæãã¦ãããEnumeratorãè¿ãã
each_sliceãcombinationçã«ã¡ã½ãããã§ã¤ã³ã§ç¹ãã¦ä½¿ãã
Enumerator#with_object(obj)
with_indexã¨åæ§ã§å¼æ°ã§æ¸¡ãããªãã¸ã§ã¯ããä¸ç·ã«åæãã¦ãããEnumeratorãè¿ãã
åããã¡ã½ãããã§ã¤ã³ã§ä½¿ãã
Hash
Hash[*key_and_value]
ãã¼ã¨å¤ã®ãã¢ã®é
åããããã·ã¥ãçæããæã«ä¾¿å©ã
(8/15 追è¨)
alist = [[1,"a"], [2,"b"], [3,["c"]]] p Hash[*alist.flatten(1)] # => {1=>"a", 2=>"b", 3=>["c"]} p Hash[alist] # => from 1.9 {1=>"a", 2=>"b", 3=>["c"]}
Hash#default=(value)
ããã·ã¥ã«ããã©ã«ãå¤ãè¨å®ããæã«ä½¿ãã
è¦ç´ ãç¡ãæã¯ã¨ããããtrueè¿ãã¦æ¬²ããã¨ãã®æã«ã
Hash#reject
Hashã§ãrejectã使ããã
key, valueã®ã©ã£ã¡ãåãåãã®ã§ä¸¡æ¹ã®å¤ã§è©ä¾¡ã§ããã
MatchData#captures
æ£è¦è¡¨ç¾ã®é¨åããããé
ååããã
Regexp.last_matchã§åå¾ãã¦$1, $2ãé
åã¨ãã¦å¦çãããªã©ã
é¨åããããEnumerableã«åãããæã¨ãã
Object
Object#method(method_name)
ã¡ã½ãããåå¾ãã¦ããããã¯ã¨ãã¦æ¸¡ãããã§ããã
yuroyoroããã®ããã°ã§ã¯é¢æ°ã渡ãã£ã½ãå½¢ã§ãããã¯ãå¦çããä¸é£ã®è¨äºãããã®ã§åèã«ãªãã¾ãã
http://yuroyoro.hatenablog.com/entry/2012/08/08/201720
Object#instance_eval, Object#instance_exec
Procãªãã¸ã§ã¯ãã¨çµã¿åãããã°ãã³ã³ããã¹ããèªå¨ã«æä½ã§ããã
å
ã«procãæ§ç¯ãã¦ããã¦å¾ããä»»æã®ã³ã³ããã¹ãã§è©ä¾¡ãããããªæã«è¯ã使ãã
Numeric
Numeric#step(limit, step=1)
ä»»æã®æ°å»ã¿ã§ã«ã¼ãããããæã«ã
Enumeratoråãã¦ãwith_objectã¨çµã¿åãããããã§ããã
String
String#self % args
sprintfãã·ã³ãã«ã«æ¸ãæ¹æ³ã
p "i = %d" % 10 # => "i = 10"
String#center(width, padding = ' '), String#ljust(width, padding = ' '), String#rjust(width, padding = ' ')
æååãä¸å¤®(å·¦å³)ã«æãããpaddignã§åããæååãæå®ã§ããã
ãã°åºåã®åºåãçã«ã
Enumerable
Enumerable#collect (Enumerable#map)
é
åã®åè¦ç´ ãå¤æããæ°ããé
åã欲ããæã¯ãeachãããã£ã¡ã使ããã
Enumerable#select (Enumerable#find_all)
é
åããã£ã«ã¿ã¼ããæã«ä½¿ããã¨ãå¤ãã
mapãã¦selectããã¨ããselectãã¦ããmapãããªã©ã
ã¡ã½ãããã§ã¤ã³ã§é
åãå¦çããã®ãæ°æã¡ããã
Enumerable#cycle(n)
ä»ã®éåãç¹°ãè¿ãããããã¯ã渡ããEnumeratorã
nãçç¥ããã¨ç¡éã«ã¼ããåºæ¥ãã
#zipã¨çµã¿åããããã
Enumerable#chunk
åæã§ãããã®ããããã¯ã§è©ä¾¡ãããæ¡ä»¶ãå
ã«åºåã£ã¦åå²ããã
ä½ãã«è²ã
ã§ããã®ã§ãããã¥ã¢ã«ãèªã¾ãªãã¨è¯ãåãããªãã
ã½ã¼ãæ¸ã¿ã®ç©ãåé¡ãã¦ã«ã¦ã³ãããããããã¹ãã空è¡ãã¨ã«æ®µè½åããããçã
Enumerable#flat_map
mapã®ä¸ã§é
åãè¿ãã¨äºéé
åã«ãªããããªãã®ãçµåãã¦è¿ãã¦ãããã
å¾ããflattenã¨ããããªãã¦ããã®ã§ãèªã¿ãããã
Enumerable#take(n), Enumerable#take_while {|elem| }
é
åããæ大nååãããã¨ããæã
足ããªããã°ããååã£ã¦ããããå¯ä½ç¨ãç¡ãã®ã§æ°æ¥½ã«ä½¿ããã
sort_byãã¦take(n)ã§ä¸ä½nåãåå¾ããã¨ãã
Enumerable#each_slice(n)
nåãã¤ãããã¯ã«æ¸¡ãã
足ããªãæã«ã¯ããã ãã渡ãã
å
¥åã2è¡ãã¤å¦çãããã¨ããé
åã3ã¤ãã¤ã®äºéé
åã«å¤æãããã¨ãã
[1,2,3,4,5,6,7,8].each_slice(3).to_a # => [[1, 2, 3], [4, 5, 6], [7, 8]]
Enumerable#group_by
ãããã¯ãè©ä¾¡ããçµæããã¼ã¨ãã¦ãã°ã«ã¼ãã³ã°ãããè¦ç´ ãé
åã¨ãã¦æã¤ããã·ã¥ãè¿ãã
ãªããåé¡ããããªã¨æã£ãæã¯å¤§ä½ä½¿ããã
Enumerable#all?, any? none?
eachã§éåã®æ¤è¨¼ãããã«ããã®è¾ºã®ã¡ã½ããã使ãã°ãã£ã¨è¦æãæ¸ããã
(8/15 追è¨)
Method
Method.source_location
ãã®ãªãã¸ã§ã¯ãã示ãã¡ã½ãããå®ç¾©ããã¦ããã½ã¼ã¹ã³ã¼ãã®ãã¡ã¤ã«åã¨è¡æ°ãé
åã§è¿ãã
ãã®ã¡ã½ããã©ãã«ãããã ããã£ã¦æã«ãµã¯ã£ã¨èª¿ã¹ãããã
(Thanks! id:sora_h)
User.method(:where).source_location # => ["/home/joker/railsapp/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.6/lib/active_record/querying.rb", 8]