ãã®è¨äºã¯ãESM Advent Calendar 2021ãã®1æ¥ç®ã®è¨äºã§ãã
RuboCop ã®ããã¯ã¨ã³ãã§ã¯ãRuby ã解æã㦠AST (æ½è±¡æ§ææ¨) ã® Ruby ãªãã¸ã§ã¯ãã¨ãã¦æ±ããããã«ãã Parser gem ã使ã£ã¦ãã¾ããParser gem ã®ã¡ã³ããã³ã¹ã¯ãRuby ã® parse.y ã«å ¥ã£ãæ§æã®ã¢ãããã¼ããå ã«è¡ã£ã¦ãã¾ããæ¬ã¨ã³ããªã§ã¯ parse.y ã®è¦³å¯ãå ã«ãã¦ãã¯ãªã¹ãã¹ã®ãªãªã¼ã¹äºå®ã¾ã§ã«ã¦ã³ããã¦ã³ã¨ãªã£ã¦ãã Ruby 3.1 æ§æã®ã¢ãããã¼ã (2021å¹´12æ1æ¥æç¹) ãã¾ã¨ãã¦ã¿ã¾ãã
ææã¨ãã¦ã¯ãRuby 3.1 ã§ã¯ããã¤ãã®æ§æã¸ã®çç¥è¨æ³ãä¸å¿ã«å°å ¥ãããã¨ãã£ãå°è±¡ã§ãã
æ°æ§æ
Hash ãªãã©ã«ã®å¤çç¥ã®æ§æã追å ããã
ruby/ruby@c60dbcd ã§å°å ¥ããã¾ããã
å¤ãä»£å ¥ããå¤æ°ã Hash ãªãã©ã«ã®è¦ç´ ã«è¨ãéã«ãå¤æ°åã Hash ã®ãã¼ã¨ãã¦ãå¤ãçç¥ã§ããããã«ãªãã¾ãã
a = 1 b = 2 {a:, b:} #=> {:a=>1, :b=>2}
以ä¸ã¨åæã§ãã
a = 1 b = 2 {a: a, b: b} #=> {:a=>1, :b=>2}
å人çã«ã¯ãES6 ã®ããããã£ã®ã·ã§ã¼ããã³ãã¨ããä¾ãããããã¼ã¨å¤ã "DRY" ã«ã§ããã®ã§è¯ãããã¨ããå°è±¡ãæã£ã¦ãã¾ãããæ示çã«æ¸ãã¨ããè¦æ¹ã®å¥½ã¿ãããããï¼ã¨ãã£ãå°è±¡ã§ãã
å¿åãããã¯ç§»è²ã®æ§æã追å ããã
ruby/ruby@4adb012 ã§å°å ¥ããã¾ããã
ãããã¯å¼æ°ã移è²ããéã«ãããã¯å¤æ°åãçç¥ã§ããããã«ãªãã¾ãã
def foo(&) bar(&) end
Ruby 3.0 ã¾ã§ã¯å¼æ°ã«ä½ãååãå¿ è¦ã§ãã
def foo(&block) bar(&block) end
å人çã«ã¯ã&block
ã &proc
以å¤ã»ã¨ãã©ååãã¤ãããã¨ãªããããªã®ã§ããããã£ãæ
å ±è½ã¡ããªã (ãã¶ãã»ã¨ãã©ã®å ´å) ã¯ãã® &
ã¨ãã象徴ã§ååè¯ãããã«æã£ã¦ãã¾ã (移è²å°ç¨ã¨ããã®ãå°ãæ®å¿µã«æããããè¯ããã) ã
ãªããå¿åãããã¯ç§»è²ã®ã¡ã½ããå®ç¾©ã®å¤ã§ bar(&)
åç¬ã§ä½¿ããã¨ãã㨠no anonymous block parameter
ã¨ã©ã¼ãçºçãã¾ãã
ãã¿ã¼ã³ãããã« pin ãªãã¬ã¼ã¿ã¼ã追å ããã
ruby/ruby@2186347 㨠ruby/ruby@fa87f72 ã§å°å ¥ããã¾ããã
ãã¿ã¼ã³ãããã®æ°æ§æã¨ãªã pin ãªãã¬ã¼ã¿ããµãã¼ãããããã«ãªãã¾ãã大å
ã®ãµã³ãã«ããæã£ã¦ãã㨠^
ã使ã£ãå¼ããããªæãã§ãã¿ã¼ã³ãããä¸ã§åããããã«ãªãã¾ãã
Prime.each_cons(2).lazy.find_all { _1 in [n, ^(n + 2)] }.take(3).to_a #=> [[3, 5], [5, 7], [11, 13]]
æ¢åæ§æã®æ¡å¼µ
endless ã¡ã½ããå®ç¾©ã®æ¬ä½ã¡ã½ããå¼ã³åºãã®æ¬å¼§ãçç¥å¯è½ã«ãªã
ruby/ruby@31794d2 ã§å°å ¥ããã¾ããã
endless ã¡ã½ããå®ç¾©ã®æ¬ä½ã¡ã½ããå¼ã³åºãã®æ¬å¼§ãçç¥å¯è½ã«ãªãã¾ãã
def foo = puts 'Hello'
Ruby 3.0 ã¾ã§ã¯å¼æ°ã«ã«ãã³ãå¿ é ã§ãã
def foo = puts('Hello')
1è¡ãã¿ã¼ã³ãããã®ã¡ã½ããå¼ã³åºãã®æ¬å¼§ãçç¥å¯è½ã«ãªã
ruby/ruby@ecb6d6a ã§å°å ¥ããã¾ããã
1è¡ãã¿ã¼ã³ãããã®ã¡ã½ããå¼ã³åºãã®æ¬å¼§ãçç¥å¯è½ã«ãªãã¾ãã
[1, 2] => a, b
Ruby 3.0 ã¾ã§ã¯å³è¾ºã«ã«ãã³ãå¿ é ã§ãã
[1, 2] => [a, b]
Arguments Forwarding ã®ã¡ã½ããä»®å¼æ°ã®ã«ãã³ãçç¥å¯è½ã«ãªã
ruby/ruby@13a9597 ã§å°å ¥ããã¾ããã
Arguments Forwarding ã®ã¡ã½ããä»®å¼æ°ã®ã«ãã³ãçç¥å¯è½ã«ãªãã¾ãã
def foo a, ... end
Ruby 3.0 ã¾ã§ã¯ä»®å¼æ°ã«ã«ãã³ãå¿ é ã§ãã
def foo(a, ...) end
ã¾ãããã©ã«ãå¼æ°ã¨ Arguments Forwarding ãä¸ç·ã«å®ç¾©ã§ããããã«ããªã£ã¦ãã¾ãããããã¯ã«ãã³ã®æç¡ã«é¢ããããRuby 3.0 ã§ã¯æ§æã¨ã©ã¼ã ã£ããã®ã§ãã
def foo(a, b = 42, ...) end
ããã§ç´¹ä»ããæ§æã¢ãããã¼ã㯠Parser gem 3.0.3.1 ã¾ã§ã§å¯¾å¿æ¸ã¿ã§ãRuboCop ã§ä½¿ããã¨ããã¨å¿åãããã¯ç§»è²ã§ã¨ã©ã¼ã«ãªãã®ãç¾ç¶ã§ããä¿®æ£ããã㯠PR éä»æ¸ã¿ã§ãã®ã§ãã¯ãªã¹ãã¹ãªãªã¼ã¹ã«ã¯ RuboCop ã§ãã²ã¨ã¨ããã® Ruby 3.1 æ§æã¯ä½¿ããããã«ãªã£ã¦ããã¨æãã¾ãã
ãªããRuby 3.1 ã¯éçºçã¨ãªãã¾ããã¡ãã£ã¨è©¦ãã¦ã¿ããã¨ããå ´åãrbenv ã§ããã°ä»¥ä¸ã§ã¤ã³ã¹ãã¼ã«ãã§ãã¾ãã
% rbenv install 3.1.0-dev
以ä¸ãRuby 3.1 ã®å è¡ãã¬ãã¥ã¼è¨äºã§ãããã¯ãªã¹ãã¹ãªãªã¼ã¹ã楽ãã¿ã§ããã
ãESM Advent Calendar 2021ãã® 2æ¥ç®ã¯ @m_pixy ã®ã¨ã³ããªã§ãã