ãªããåãããªè¦åºãã§ãããruby 1.9 ã主ã«ä½¿ãããã«ãªã£ã¦ 1 å¹´ã¡ãã£ã¨çµã£ãã®ã§ã1.9 ã®æ°æ©è½ã«æãã¨ããã注æç¹ãªã©ãæ¸ãæ®ããã¨æãã®ã§ããããã« 1 å¹´å¾ã«è¦ãã¨ããããã®ããã¯ãããªãµãã«èãã¦ããªãããªã©ã¨ææ
¨ã«ã²ããäºå®ã§ãã
ãããããæã£ã¦ããã¨ãã¼ãã® ruby 1.9 çµé¨ã¯ãã¹ã¦è¶£å³ç¯å²ãªã®ã§ãã¨ã³ã¿ã¼ãã©ã¤ãºã¨ãã·ã¹ãã éç¨ã®åé¡ã¨ãã¯ç¥ãã¾ããããã¨ã¼ã㯠ruby ã®ã³ããã¿ãªã®ã§ãè²ç¼é¡ãããã¨æãã¾ãããããããã
YARV
VM å®è¡ã«ãªã£ãã¨ãã話ãä¸è¬çã«ã¯ãéããã¨ããæèã§èªããã¾ãã1.8 ã¨æ¯ã¹ãã¨ç¢ºãã«éãã§ããã§ãã1.9 ã°ãã使ãåºãã¨ãªãã¨ãæããªããªãã¯ãã§ããéããªãã¦ç¸å¯¾çãªä¾¡å¤ã§ããããå½ç¶ã§ããã©ãã好æçã«èããã°ãããªãã¨ãæããªãç¨åº¦ã«ãé
ãã¦å°ããã¨ãæ¸ã£ããã®ãããããªãã
ã³ã³ãã¤ã«ãã§ã¼ãºãæããããeval ã require ãé
ããªã£ããã¨ã«ã¯æ³¨æãå¿
è¦ã§ããã¨ãã«ãããã£ã±ã require ãã¦ããã«çµããããã°ã©ã ãã¯å²ããé£ãã¾ã (ä¾: gem ã rake ã®ãããªã³ãã³ããçã® cgi ãªã©) *1 ã
ã¾ããéããªãã¦é£¾ãã§ããã *2 ãå人çã«ã¯ã1.8 ã®ãªãããã¡ããã¡ãããã½ã¼ã¹ã³ã¼ããããããªãã«æç§æ¸çãªãæ´çãããã½ã¼ã¹ã³ã¼ãã«ãªã£ããã¨ã®æ¹ãå¬ãã *3 ã
M17N
ãªããã幸ã Ruby ã©ã¤ããããã¾ããé¢åãªå¥´ãã¨ããå å ¥è¦³ããããã©ãå®éã«å°ã£ããã¨ã¯ãã¾ããªãã§ãã
- Q. magic comment ã®è¦ãç®ãããµããã
- A. ã¼ãã®ä¸»è¦³ã§ã¯ãããµãã®ã¯ magic comment èªä½ã§ã¯ãªã emacs ã¹ã¿ã¤ã« ã主㫠-*- ãã©ã -*- ã®é¨åãRuby ã¨ãã¦ã¯ # coding: UTF-8 ã ãã§ããã®ã§ãæ £ããã°ãããã許容ç¯å²ã§ãã
- Q. magic comment ãæ¸ããã¨èªä½ãé¢åãããã
- A. æå¤ã«ãæ £ãã¾ã *4 ããã¨ãã³ã¡ã³ãããªãã©ã«ã«æ¥æ¬èªãæ¸ããªããã°ä¸è¦ãªã®ã§ãã¯ã³ã©ã¤ãã¼ãªããã®éªéã«ã¯ãªããªããã¡ãªã¿ã«ãã©ã¤ãã©ãªã®ã½ã¼ã¹ã§ã¯ãããæ¸ãã¹ãã§ãªãããã *5 ã
- Q. æååå¦çãé ããªãã¨ããåã
- A. å®éã«å°ã£ããã¨ã¯ãªãã§ããä½åº¦ããM17N ãããã«ããã¯ãããªãããã¨çã£ããã¨ããããã©ãä»ã®ã¨ããå ¨é¨æ¿¡ãè¡£ã ã£ãã
- Q. ã¨ã³ã³ã¼ããç°ãªãæååãé£çµã»æ¯è¼ããã¨ä¾å¤ã«ãªããã ã£ãï¼
- A. ä»ã®ã¨ãã UTF-8 㨠EUC-JP ã¨ãã¤ã㪠(ASCII-8BIT) ãã使ã£ã¦ãªããã©ãå¦ãªä¾å¤ã«ééãããã¨ã¯ãªããASCII-8BIT ã US-ASCII ã¯ããã«è¨ãã£ã¦ãããã¿ããã
- Q. ä»æ§ããªããããããããªãã
- A. ããã¯ã¼ããããããªãããã©ãRuby ã®ä»æ§ãªãã¦ãããã£ã¦ãæ°ã«ãªã£ã¦ãããã£ã¦ãªããã¨ã°ã£ãã§ããã
ãã£ã±ãã"ããããã"[0] #=> "ã" ã¯ä¾¿å©ã ãã"ããããã".encode("EUC-JP", "UTF-8") ã便å©ã§ããã
ãã ãç°å½ã§æ¸ãããã³ã¼ãã M17N ã®ããã§èªåã®ç°å¢ã§åããªãã¨ããM17N ã«å¯¾å¿ã§ãã¦ãªãå¤ãã©ã¤ãã©ãªã§ã¯ã¾ãã¨ãã¯ä»å¾ãçºçããããã
Fiber
Coroutine ã SemiCoroutine ããååããã£ããããã§ãæ¬æ°ã§ä½¿ã£ããã¨ã¯ãªãã
Fiber ã¨è¨ãã°ã²ã¼ã ã®ç¶æ
管çã ãã©ã1.9 ã® Fiber 㯠1.8 ã®ã¹ã¬ããã¨åãä»çµã¿ã§åãã¦ãã (= é
ã) ã®ã§ãããã¾ãåãã¦ãªããããå¤é¨ã¤ãã¬ã¼ã¿åã«ã¯ Enumerator#next ãããã®ã§ Fiber ã使ããªãã¦ããããªã®ã§ãæ®éã®äººã Fiber ãç´æ¥ä½¿ããã¨ã¯äºä¾ã¯ãã¾ãæ³åã§ããªã *6 ã
Fiber ã®ã¹ã¿ãã¯é·ã®å¶éããã¤ãã®ã¯æ³¨æãå¿
è¦ãã *7 ãã§ã Fiber ã 1.8 ã®ã¹ã¬ãããã軽éãªã®ã¯ãã®å¶éã®ããããªã®ã§ããããããã¨ããã
ã¡ãªã¿ã«ãç¥ã人ãç¥ã Fiber::Core ã¯é»éè¡ãªã®ã§ä½¿ã£ã¦ã¯ãããªã (ruby-dev:31601) ã使ããªãã絶対使ããªãï¼
Enumerator (é 延é åçãªãã®)
1.9 æ大ã®ä¾¿å©æ©è½ããã¤ãã¬ã¼ã¿ã£ã½ãã¡ã½ããããããã¯ãªãã§å¼ã³åºã㨠Enumerator ãè¿ããã¨ãã convention (æ £ç¿ããããã) ãå°å ¥ãããã
a = [] [1, 2, 3].each_cons(2) do |x, y| a << [x, y] end a.map {|x, y| ... }...
ã
a = [1, 2, 3].each_cons(2).map {|x, y| ... } ...
ãªã©ã¨æ¸ãããç´ ç´ã«ã¡ã½ãããã§ã¤ã³ãã¤ãªããããã®ã§ãã¨ã£ã¦ãããããã
ä¸å¹´ãããåã«ãこのしきたりの一貫性のなさや非互換性に対して文句たれまくったãã§ãããå¤ãã®å ´åã§ã¯ã¨ã¦ã便å©ãªã®ã§ä½¿ãã¾ãã£ã¦ãã¾ã *8 ãããããè¨è¨ã®ãã©ã³ã¹æè¦ã¯ matz magic ã§ãã *9 ã
ãã¨ããEnumerator 㯠Fiber ã§å®è£ ããã¦ãã (ããã«é ã)ãã¨è¨ãåã¯ã次ã«æãã Enumerator#next ã ãã®è©±ãé 延é åã¯å¥ã«ç¹ã«é ãã¯ãªãã®ã§ããããããã使ãã¾ãããã
Enumerator#next (å¤é¨ã¤ãã¬ã¼ã¿)
Enumerator ã each ããã®ã§ã¯ãªããyield ãããå¤ãä¸ã¤ãã¤åãåºããã
g = [1, 2, 3].each p g.next #=> 1 p g.next #=> 2
æåã«è¦ãã¨ãã¯ä½¿ãæãããããã ã¨æã£ããã©ã1 å¹´ã®éã«ã¾ã¨ãã«ä½¿ã£ãè¦ãã 1 度ããªããPython ããã®ç§»æ°ã¯ä½¿ãã®ããªã
å
é¨å®è£
㯠Fiber ãªã®ã§ãã¨ã£ã¦ãé
ããã¨ã«æ³¨æã300,000 åã® next ã« 2 ç§ããããããã
Array ã®æ¡å¼µ
Array#permutation ã Array#product ã¯ã¨ã¦ããã使ããæ¢ç´¢ãç·å½ããããããã¨ãã¨ãããã¼ãã«ãä½ãã¨ãã¨ãã
Array#flatten ã«å¼æ°ã追å ãããã®ãã¨ã¦ã便å©ã§ã (参考) ã
ãã¨ã¯ãArray#drop ã Array#take ãArray#shuffle ãArray#sample ã便å©ã
é åãæååã®çã¡ã¢ãªå
é·ã 3 以ä¸ã®é
åããé·ã 11 以ä¸ã®æåå *10 ãªã©ããã¡ãã¡ malloc ãããªããªã£ãã®ã§ãå°ããªé
åãå°ããªæååãå¤ç¨ããã¨ãã®ã¡ã¢ãªå¹çããããªã£ã¦ãã (参考) ã
æ©è½ã¨ããããæé©åãªã®ã§ãã¦ã¼ã¶ãç´æ¥æèããããã¨ã¯åºæ¬çã«ã¯ãªãã¯ããã§ããé¢æ°åããã°ã©ãã³ã°ã§ãããããã¿ãã«ããããã¢ãã®ä»£ããã«é
åã使ã人ã¯çµæ§ããã¯ãã§ããããã人ã¯åãã§ããã¨æããã¼ãã¯ウォーキングの路線図ã®çæã 10% ãããéããªã£ã (2 åå¼±ãã 1 åå¼·ããã) ã®ã§ããã®ãããããããã
ã¨ããã§ã1.8 ã§åãã¦ããæ¡å¼µã©ã¤ãã©ãªã 1.9 ã§
error: âstruct RStringâ has no member named `ptr'
ã¿ãããªãã«ãã¨ã©ã¼ã«ãªã£ããããã®æé©åãåå ã§ããããããæã¯è½ã¡çãã¦
- RARRAY(...)->ptr ã RARRAY_PTR(...) ã«æ¸ãæãã
- RARRAY(...)->len ã RARRAY_LEN(...) ã«æ¸ãæãã
- RSTRING(...)->ptr ã RSTRING_PTR(...) ã«æ¸ãæãã
- RSTRING(...)->len ã RSTRING_LEN(...) ã«æ¸ãæãã
ã¨ããã°ããã§ãã
rubygems ã rake ã®çµã¿è¾¼ã¿å
ã¼ã㯠ruby 1.8 ã®ãã㯠gem ã使ã£ã¦ããªãã£ã *11 ãã§ããã1.9 ã§ã¯ä½¿ãããã«ãªãã¾ãããgem install ã§ããããªã©ã¤ãã©ãªãéã¹ãã®ã¯æ¥½ããã§ããã
ãã ãä¸ã®å¤ãã® gem 㯠1.9 ã«å¯¾å¿ãã¦ããªãã®ã§ãæ¬å½ã«æ¥½ãããªãã«ã¯æéãå¿
è¦ããã§ããããè¦ãåé¡ã¯ã
- åè¿°ã® RARRAY_PTR ã®è©±
- when 1: ã®ãããªã³ãã³ãæ¸ããªããªã£ããã¨
- String ã Enumerable ã§ã¯ãªããªã£ãã㨠(String#each ãæ¶ãã)
ã§ãããã
ã¡ãªã¿ã«ã1.9 ã§ä½¿ããç´ æ´ããã gem ã§ã¼ããæ¥ãããä¸è©±ã«ãªã£ã¦ããã®ã¯ãcairo 㨠ramaze ã
rake ã¯ããã¾ã使ã£ã¦ãªããã©ãããªãã ã¡ãã£ã¨ã¦ã©ã¼ãã³ã°ã®ãã¼ã¿çæã»ç®¡çã¹ã¯ãªããã¨ãã¦ä½¿ã£ã¦ã¿ãããã¨ã¦ã便å©ã ã£ããä»å¾ãã£ã¨ä½¿ããã
æ£è¦è¡¨ç¾ã®å¾æ¹åç §ã«ååã使ãããã¨
æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ã鬼è»ã«ãªã£ã¦ãä¸çªä½¿ããããªæ©è½ã
"Yusuke Endoh"[/(?<first_name>\w+)\s+(?<last_name>\w+)/] p $~[:first_name] #=> Yusuke p $~[:last_name] #=> Endoh
ããã«ã便å©ãããªãã§ãããå®éã«ä½¿ã£ããã¨ã¯ãªãã§ãããã£ã±ãæ£è¦è¡¨ç¾ãç
©éã«ãªãããããªãã
æ£è¦è¡¨ç¾ã£ã¦åºæ¬çã«ä½¿ããããªããã§ããããç°¡åãªãã¼ã¹ã«ã¯çåå²é¶ã ã¨æãããè¤éãªãã¼ã¹ã«ã¯ä½¿ããªãããæ®éã®ãã¼ã¹ã§ãã½ã¼ã¹ã®ã¡ã³ããã³ã¹æ§ãä¸ããããæ¸ãæ¨ã¦ããã°ã©ã ã§ãã使ããããªãã®ã§ãããããããæå³ã§ã¯ãString#start_with? ã¿ããã®ãããããã
Hash ã«é åºãã¤ãã
Hash ã each ããã¨ããæ°è¦ä»£å ¥ããé ã«åæãããã
h = {} %w(foo bar baz).each {|k| h[k] = true } h.each_key {|k| p k }
ãªã©ã¨ããã¨ã1.8 ã§ã¯ "foo" "bar" "baz" ã®åºã¦ããé åºã¯ä¸å®ã ã£ãããã©ã1.9 ã§ã¯ãã®ã¨ããã«åºã¦ãããã¨ãä¿è¨¼ããã¾ã *12 ã
çµé¨ä¸ãããã«ä¾åããã³ã¼ãã¯èªã¿ã«ããã§ããã§ããããã·ã¥ã¨é
åã®çµã§ç®¡çããå¿
è¦ããªããªã£ã¦ã幸ããªã®ã¯å¹¸ããªãã§ãããã諸åã®å£ã
ã¼ãã®ãã¹ããã©ã¯ãã£ã¹ã¨ãã¦ã¯ããªã«ãã®ãã¼ãã«ã¿ããã«ãããã£ããåæåããå¾ã§è¿½å ã»æ´æ°ããªãããã·ã¥ãã«ã ã使ãã®ãããã¨æã£ã¦ã¾ããåæåãããã¨ã« freeze ããã¨ãããå®å
¨ã
代å
¥é ã ãã§ãªãããã£ã¨ç´°ããé åºãå¶å¾¡ã§ãããããããããªããã¨æããã©ãAPI ã®è¨è¨ãå¹ççãªå®è£
ãé£ãããã
ãããã¯å¼æ°ä¸ã®ãããã¯å¼æ°
f = lambda {|&block| block.call(1) } f.call {|x| p x } #=> 1
ã¨æ¸ããããã«ãªã£ããä»ã®ã¨ãããæ¬æ°ã§ä½¿ã£ããã¨ã¯ãªãæ©è½ã
define_method ã§ãããã¯ãåãåãã¡ã½ãããå®ç¾©ã§ããããã«ãªã£ãã®ã¯ããªãã¬ã¯ã·ã§ã³ããã¢ã«ã¯ããããããã
define_method(:foo) do |&block| block.call end
ä¸ã®ã³ã¼ã㯠def foo; yield; end ã¨åçã§ã (ç´°ãããã¨ãæ°ã«ããªããã°) ã
->(args) { ... }
lambda {|args| ... } ã®å¥è¡¨è¨ãåå¹´åãããã¾ã§ã¯ lambda {|&block| ... } ã syntax error ã§ããã->(&block) { ... } ã¨æ¸ããããªãã£ãã®ã§ã-> ã¯å¿
é ã®åå¨ã ã£ããã§ãä»ã§ã¯ lambda {|&block| ... } ã¨æ¸ããã®ã§ãåå¨æ義ã失ã£ãã¨ããåãããã
å®éã«ä½¿ããã©ããã¯ãå人ã®å¥½ã¿æ¬¡ç¬¬ã ã¨æããproc ã lambda ã使ããã©ããèªä½ããå人ã®è¶£å³ã«å¤§ããä¾åããããã
ã©ãã§ããã話ã¨ãã¦ã¯ãRuby 1.9 ãè¨å·ã ãã§ãã¥ã¼ãªã³ã°å®å
¨ã«ããããã«éè¦ (記号だけで brainfuck インタプリタã記号だけで任意の文字列を eval) ã
post argument
def f(a, b, c, *rest, x, y, z) ... end
ã¨æ¸ããããã«ãªã£ããã¼ãããã¨ã½ã¼ã¹ãããã£ã¦ãã¨ãç¡æèã®ãã¡ã«ä½¿ã£ã¦ãããã¨ãããæ©è½ãä»ã¨ãªã£ã¦ã¯ã1.8 ã§æ¸ããªãã£ããã¨ãä¸èªç¶ã«æããã
ãã¨é
åã®å
é ã¨çµç«¯ãåãåºãã¨ã
first, *, last = ary
ã¨æ¸ããã¨ããã°ãã°ããã
ã©ãã§ãä½åº¦ã§ã splat
post argument ã«ä¼¼ã¦ããã©ã
ary1 = [1, 2, 3] ary2 = [4, 5, 6] foo(*ary1, *ary2)
ã¨æ¸ããããã«ãªã£ãããããããç¡æèã®ãã¡ã«ä½¿ã£ã¦ãããã¨ããã¼ãã«ã®ä½æã¨ãã§ãé åãªãã©ã«ã®ä¸ã§ãã使ãã
chars = [*"A".."Z", *"a".."z", *"0".."9"]
ã¾ã¨ã¾ããªãã¾ã¨ã
ãã¥ã¼ã¹ã§ã¯ãã£ã±ã YARV ã¨ã M17N ã¿ãããªå¤§ããæ©è½ãåããããããã¡ã§ãããã¼ã¸ã£ã¼ã¨ãã·ã¹ãã 管çè
ã¨ãã®ãããªç«å ´ã®äººã«è¨´ããã«ã¯ãããã ãããã©ãå®éã«ããã°ã©ã ãæ¸ã人ã«ã¨ã£ã¦ã¯ãããªã®ç©ºæ°ã§ãããã¼ããã¯ã¯ã
ã¨ããããã§ã¼ãã好ããªæ°æ©è½ãã¹ã 3 ã
- Enumerator (é 延é åçãªãã®)
- Array ã®æ¡å¼µ
- é åãæååã®çã¡ã¢ãªå
ãããã®ãããã§ãã ãã¶é¢æ°åããã°ã©ãã³ã°ããããããªã£ãã¨æãã¦ã¾ããã¨ããããã¼ããç¥ã£ã¦ãè¨èªã®ä¸ã§ã¯ãHaskell ã®æ¬¡ã«ãªã¹ãå¦çãããããè¨èªãã§ã *13 ã
ãããªè¨äºããè¦ãã¹ã 1.9 ã®æ°æ©è½ç´¹ä»æç®
追è¨
"ããããã".encode("EUC-JP", "UTF-8") ã encoding ã«ãªã£ã¦ãã¨ãããªã©ãä¿®æ£ã
*1:ãã¤ãã³ã¼ãåºåã»èªã¿è¾¼ã¿ãã§ããããã«ãªãã°æ¹åãããããããªãã
*2:Project Euler ã§ã¯ã¨ã¦ããä¸è©±ã«ãªã£ããã©ã
*3:ãã ã dfp[-2] ã®ãããªãYARV ã®ãã¼ã¿æ§é ãç¥ããªãã¨æå³ä¸æ㪠magic number ãã¡ãã°ããããã®ã§ããªãã¡ã¯ã¿ãªã³ã°ã¯å¿ è¦ã ã¨æãã
*4:æ æ°ãã®ãã©ã¡ã¼ã¿ãããªãé«ãã¼ããè¨ããã ããééããªãã§ãã
*5:ã©ã¤ãã©ãªã¯ããããªã¨ã³ã³ã¼ãã£ã³ã°ã®ã½ã¼ã¹ãã require ããããããç¹å®ã®ã¨ã³ã³ã¼ãã£ã³ã°ã«ç¹åãã¹ãã§ãªãããã ã YAML ãã¼ãµã¿ããã«ãç¹å®ã®ã¨ã³ã³ã¼ãã«å¼·ãé¢ä¿ãããã©ã¤ãã©ãªã§ã¯æå®ããã®ãããã
*6:å人çã«ã¯ Fiber ã®å ¨æ©è½ã Enumerator ã«å ¥ãã¦ãã¾ããFiber ã¯è¡¨ã«è¦ããªãã§ãããã£ãã®ã§ã¯ãªããã¨æã£ã¦ãã (ruby-dev:31798) ã
*7:ãããã¬ãã«ãªãå帰å¼ã³åºãã 8000 åãããããããã©ãFiber ã®ä¸ã 㨠300 åãè¡ããã« SystemStackError ã«ãªãã
*8:èªåã§ãæ°ãã¤ããªããã¡ã«å¿å¤ãããã¦ããã§ããããã®è¨äºãæ¸ããã¨æã£ãã®ã¯ãããããã£ããã
*9:ä»ã§ãä¸è²«æ§ã®ãªããæ°ã«ãªããªãããã§ã¯ãªããã§ãããary.inject.with_index ã¨æ¸ããªãã®ã¯æ²ããããary.each_cons(2).to_a.compact ã®ããã«ãã¡ãã¡ .to_a ãã¯ãã¾ãªãã¨ãããªããã¨ãããã®ãé¢åããã®è¾ºã¯ä»å¾ 2.0 ã® lazy array ã«åãã£ã¦ã¡ãã£ã¨ãã¤è§£æ±ºãã¦ããã®ã ããã
*10:32 bit ç°å¢ã®å ´åãå®éã«ã¯ (sizeof(void*) * 3 / sizeof(char) - 1) 以ä¸ã®æååã対象ãªã®ã§ã64 bit ç°å¢ã®å ´åã¯é·ã 23 以ä¸ã®æååããªããã¶ãã
*11:Debian (ã¨ããã FHS) ã¨ã®ç¸æ§ãæªããã§ããããã
*12:ãã ã Ruby ã®è¨èªä»æ§ã¨ããããã§ã¯ãªããruby 1.9 ã®å®è£ ä¸ã®ä»æ§ã¨ããæ±ãã ã£ãæ°ãããã
*13:Haskell ã»ã©ã®ã«ãã³ããã¯ãªããã§ããããã§ã OCaml ããã¯æ ¼å¥½ããã