ref: http://gihyo.jp/dev/serial/01/ruby/0005 (Ruby Freak Lounge 第 5 å: Ruby 1.9 ã®æ°æ©è½ã²ã¨ããã (å¾ç·¨))
ã®è£è¶³ãªã©ã
鬼è»ã«ããæ£è¦è¡¨ç¾ã®å¼·å: ååã使ã£ãåç §
/(?
- ååã¤ãã§ãã£ããã£ã»åç §ãããã ãã§ãå¤æ°ã¾ã§å®ç¾©ãã¦ã»ãããªããæ¢åã®å¤æ°åã¨è¡çªãããã§æãã
- =~ ã¨ããã®ã¯ãã ã®ã¡ã½ããå¼ã³åºããªã®ã«ããªãã§å¤æ°ãå®ç¾©ãããã¨ããããã ãdef_foo(1) ã§å¤æ° foo ãå®ç¾©ãããããã¢ãã ããã«ã
- /(?
...)/ =~ string ã§ã¯å¤æ°å®ç¾©ãã¦ãre = /(? ...)/; re =~ string ã string =~ /(? ...)/ ã string[/(? ...)/] ã /(? #{ "..." })/ =~ string ã§ã¯å¤æ°å®ç¾©ããªãã®ããããããã
ã¨ããããããã¨æãã¾ããè¿çã¨ãã¦ã¯
- ã¾ãããããä»æ§ãªãã§ææ ¢ãã¦ãã ãããæ £ããã°ä¾¿å©ã§ããããââãããã§æããã§ãªããââãã¦å®éã«ã¯ã¾ã£ããã¨ããçµé¨è«ãªãèæ ®ãããããã
- æ¢åã®å¤æ°ã¨ã®è¡çªã«é¢ãã¦ã¯ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ -w ãã¤ããã°è¡çªãæãã¦ããã¾ãã
- =~ ã¯ã¡ã½ããã§ããã// =~ string ã®å½¢ã§æ¸ããã¨ãã¯å®ã¯ã¡ã½ããå¼ã³åºãã§ã¯ãªãã§ããRegexp#=~ ãåå®ç¾©ãã¦ãã// =~ string ã®æåã¯å¤ãããã¾ãããããããæå³ã§ãããã ã®ã¡ã½ãããã§ã¯ãªãã§ãã(ruby-dev:21050)
- å¤æ°ãå®ç¾©ãããæ¡ä»¶ãã¡ãã£ã¨ãããããã®ã¯ç¢ºããæ¡ä»¶å¼ä¸ã® .. ã¿ãããªè£ææ©è½ã®é°å²æ°ã¯ããã¾ãããã
ã¨ããæãã
æè¿ãã®æ©è½ãæèçã«ä½¿ãããã«ãã¦ã¾ããã³ã¼ãã®å¯èªæ§ãã¡ã³ããã³ã¹æ§ã¯ä¸ãã£ã¦ãããããªãããªãããã¶ãããã ãæ£è¦è¡¨ç¾ãé·ããªã (â 1 è¡ãé·ããªã) ã®ã¯æ¸ã楽ãããå°ãç ç²ã«ããããã¨ã
if /(?<foo>abc...)/ =~ string foo elsif /(?<foo>def...)/ =~ string foo end
ã
case string when /(?<foo>abc...)/ foo when /(?<foo>def...)/ foo end
ã«ç½®ãæããããªãã®ãä¸æºã ã£ããã¨ã 1 åããã
鬼è»ã«ããæ£è¦è¡¨ç¾ã®å¼·å: 表ç¾è½åã®åä¸
ç°ä¸å²ã¹ãã·ã£ã«ã¯ç¢ºãã«å¼·åãªãã§ãããå½¹ã«ç«ã¤ç¶æ³ãã©ã®ããããããã®ã¯æ£ç´ãããããã¾ãããæ¬å¼§ã®å¯¾å¿ã«ãã¦ãããæ¬å¼§ã®å¯¾å¿ãã¨ãã¦ãããã§ãã¯ãããããã©ããã¼ã¹ãã¦æ§ææ¨ã«ããå¿
è¦ã¯ãªããã¨ããç¶æ³ã¯ã©ã®ãããããããªãã
ã¨ããã§ä»ã® Ruby ã® (ã¨ããã鬼è»ã®) æ£è¦è¡¨ç¾ã®è¡¨ç¾è½åã£ã¦ãã£ããã©ã®ããããªãã§ãããããç°ä¸å²ã¹ãã·ã£ã«ã«ãã£ã¦æèèªç±ææ³ã¯å
å«ããã®ããªï¼
é åºä»ã Hash
2 æã® ramaze ããã®ããã§ç¡éã«ã¼ããã¦ã¯ã¾ã£ã¦ãã
ããã»ã¹ã®èµ·å
ã¼ãã¯ããã¨æè¿ã¾ã§ç¥ããªãã£ãæ©è½ãªãã§ãããããã¯ã»ãã¨ã¼ã«ä¾¿å©ã§ãããå¾æ¥ã® system ã `` ã¯æ¨æºã¨ã©ã¼åºåããæå®ã§ããªãã¦ãã¶ã£ã¡ãã使ãæ¨ã¦ã¹ã¯ãªãããããã«ãã使ããªãã£ãã§ããããopen3 ã ã® open4 ã ã®ãããè¦ããªãã
Kernel#p ãå¼æ°ãè¿ãããã«
printf ãããã° (ç¬) ãªè©±ã§ãããç¬ããã®ã¯ã¾ã¨ããªãããã¬ãç¨æããã¦ããã§ããããã¾ããããã¬ãã£ã¦ãã¼ã㯠printf ãããã°ãããã ãã©ã
ãããã printf ãããã°ã®åé¡ç¹ãèããã¨
- åºåå ãåºåãã©ã¼ããããèªç±ã«å¤æ´ã§ããªãã®ã§ä¸ä¾¿
- åºåã«ãã£ã¦ããã©ã¼ãã³ã¹ãä¸ãã
- åãè¾¼ãã printf ã確å®ã«é¤å»ããã®ãé£ãã
ããããã¨æãã¾ããããããã«å¯¾ããã¼ãã®è¿çã¯
- Ruby ã§ã¯ p ãåå®ç¾©ããã°ãåºåå ããã©ã¼ãããã¯å¤ãããã (å®éã¡ããã¡ããããã¾ã)
- p ã¯å®æåã«æ®ããããªã¡ã½ãããããªãã®ã§åé¡ãªã (ãããã°æã® p ã®ã³ã¹ãããæ°ã«ãããããªããã°ã©ã ãªã Ruby ã使ãã®ãèãç´ããæ¹ããããã)
- é¤å»ãé£ããã®ã¯ãã®éã (ruby-dev:38153 ãæ¡ç¨ãããã¨ãããªã)
ã¨ããã¨ããã§ãã
æ¹è¡ã®å ¥ããªã base64 ã®ã¨ã³ã³ã¼ãã£ã³ã°
RFC 4648 ã§ã¯ãä½è¨ãªè¨å·ãå«ãã ã¨ã³ã³ã¼ãæååããã³ã¼ããããã¨ãããæ絶ããªããã°ãªãã¾ãã (MUST) ããªã®ã§ãæ¹è¡ (ä½è¨ãªè¨å·) ãå«ãã æååã m0 㧠unpack ããã¨ä¾å¤ã«ãªãã¾ããã¡ãã£ã¨æ³¨æã
p "YWFh\n".unpack("m") #=> ["aaa"] p "YWFh\n".unpack("m0") #=> ArgumentError: invalid base64
è¨äºã«æ¸ããªãã£ããã¨
-w ãä»ãã¦å®è¡ããã¨ã対å¿ããæ§æã®ã¤ã³ãã³ãããã§ãã¯ãã¦ããã¾ããä½ãå¬ãããã¨ããã¨ã以ä¸ã®ãããªãã°ãæ¸ãã¦ãã¾ã£ãã¨ã
class Foo def foo 3.times do |x| p x end edn # typo ã®ãã㧠edn ã¨ããã¡ã½ããå¼ã³åºãã«ãªã£ã¦ãã def bar 4.times do |x| p x end end end
-w ãªãã§å®è¡ããã¨
$ ruby19 -r broken-indent.rb -e '' ruby19:0:in `require': /home/mame/work/ruby19/ruby/broken-indent.rb:13: syntax error, unexpected $end, expecting keyword_end (SyntaxError)
ã¨ããããã«ããæå¾ã®è¡ã« end ããªããï¼ãã¨ã ãè¨ããã¦ãã©ããæªãã®ããããã¾ããã§ãããã¡ã½ãããå¢ããã¨çµæ§å°ãããã㧠-w ãã¤ãã¦å®è¡ããã¨
$ ruby19 -w -r broken-indent.rb -e '' /home/mame/work/ruby19/ruby/broken-indent.rb:13: warning: mismatched indentations at 'end' with 'def' at 2 ruby19:0:in `require': /home/mame/work/ruby19/ruby/broken-indent.rb:13: syntax error, unexpected $end, expecting keyword_end (SyntaxError)
ã¨ããããã«ã2 è¡ç®ã® def ã«å¯¾å¿ãã end ã®ããããæªãããã¨ããããã¾ãã
ã¾ããdo/end ã®å¯¾å¿ãæãã¦ãããã¨ãã£ã¿ã使ã£ã¦ã人ã«ã¯ lint ç¨åº¦ã®æå³ãããªãã§ããã©ããã¼ãã vim 使ã£ã¦ãã®ã§ããã»ã©æ©æµã¯ãªãã
ã§ããªãç´¹ä»ã§ããªãã£ããã¨ããã¨ãrequire 以å¤ã§åããªãããã
$ ruby19 -w broken-indent.rb broken-indent.rb:13: syntax error, unexpected $end, expecting keyword_end
ããããã²ã¨ã¤ã®ãã°ãªãã§ããããã¾ã ç´ã£ã¦ãªããåãåå ã§ãcoverage.so ãåãã¦ãªããæ²ããã
æå¾ã«
Ruby 1.9 ã«ã¤ãã¦ã¼ãã®ç¥ãéãã®æ°æ©è½ãæ¸ã並ã¹ã¾ããããã®è¨äºãçããã®å½¹ã«ç«ã¤ãã©ããã¯ãããã¾ãããã調ã¹ã¦ããã¡ã«ã¼ãèªèº«ãåãã¦ç¥ã£ãæ©è½ãçµæ§ãã£ã¦ãå°ãªãã¨ãèªåã®ããã«ã¯ãªãã¾ãããããã£ãããã£ãã
ã¡ãªã¿ã«ä»åæ¸ããã®ã¯ããã¾ã§ã1.9 ã®æ°æ©è½ãã§ãã£ã¦ãã1.9 ã§å»æ¢ãããæ©è½ãã«ã¤ãã¦ã¯ã»ã¨ãã©è§¦ãã¦ãã¾ããã移è¡ãã¦ãã¦ã¯ãã»ã¼ 1.9 ãã使ã£ã¦ãªãã¼ãã«ã¯ãã¾ãæ¸ããªãã¨æãã®ã§ã誰ãæ¸ãã¨ããã§ãã