ãã®è¨äºã¯ Ruby Advent Calendar 2013 ã® 9 æ¥ç®ã®è¨äºã§ãã
åæ¥ã¯ aoitaku ããã® [Ruby] ãã£ãã¤ããªã㧠ã§ããã
ã¯ããã«
2.1.0-preview2 ãåºãã®ã§ãæ®æ®µä½¿ãã® Ruby ã 2.1 ã«ãã¦ã¿ã¾ããã (inspired by rbenv global 2.0.0-rc1 ãã話 - sojourn)
ã¤ã³ã¹ãã¼ã«
$ brew install readline # if you need $ (cd ~/.rbenv/plugins/ruby-build && git pull origin master) $ CONFIGURE_OPTS="--with-readline-dir=`brew --prefix readline` --enable-bundled-libyaml" rbenv install 2.1.0-preview2 $ rbenv global 2.1.0-preview2 $ ruby -v ruby 2.1.0dev (2013-11-23 trunk 43807) [x86_64-darwin12.0]
(see also: https://github.com/sstephenson/ruby-build/blob/master/share/ruby-build/2.1.0-preview2 )
æ°ã«ãªã£ãå¤æ´
NEWSãèªãã§æ°ã«ãªã£ãã¨ãããããã¯ã¢ãããã¾ãããç解ã§ãã¦ããªãã¨ããã¯é½åããæ°ã«ãªããªãã£ããã¨ã«ãªã£ã¦ãã¾ãã
ä»ã«ãå ¬å¼ã®ãªãªã¼ã¹ã¢ãã¦ã³ã¹ã«è¼ã£ã¦ãã笹ç°ããã® è³æ ã @konstantinhaase ããã® è¨äº ãåèã«ãªãã¾ã
å¤æ´å 容ã®æå³ãªã©ã追ãã®ã« nagachika ããã® PB memo ã大å¤åèã«ãªãã¾ããã
è¨èªã¬ãã«ã®å¤æ´
GC ã RGenGC ã«ç½®ãæããã¾ãã
2.1 ã®å¤§ããªå¤æ´ç¹ã§ãããããç解ã§ãã¦ããªãããå²æã 笹ç°ããã® è³æ ã詳ããã§ãã
Required keyword arguments (æ¥æ¬èªã§ã¯ãªãã¨è¨ãã°)
2.0 ã§å ¥ã£ããã¼ã¯ã¼ãå¼æ°ã¯ããã©ã«ãå¤ãå¿ é ã¨ãªã£ã¦ãã¾ããã2.1ããã¯ããã©ã«ãå¤ãªãã®ãã¼ã¯ã¼ãå¼æ°ãå¯è½ã¨ãªãã¾ããã
# Keyword arguments def hoge(a: 1) puts a end hoge a: 5 #=> 5 hoge #=> 1 def piyo(a:) puts a end piyo a: 5 #=> 5 piyo #=> ArgumentError: missing keyword: a
Added suffixes for integer and float literals: 'r', 'i', and 'ri'.
æ°å¤ãªãã©ã«ã«æ°ããsuffixã追å ããã¾ããã å人çã«ã¯ãã¾ã®ã¨ãã使ããããªã±ã¼ã¹ããªãã®ã§ããã£ã¨ã
0.5r #=> (1/2) 0.5i #=> (0+0.5i) 0.5ri #=> (0+(1/2)*i)
def-expr now returns the symbol of its name instead of nil.
ã¡ã½ãããå®ç¾©ããã¨ãã« 2.0 ã¾ã§ã¯ nil
ãè¿ã£ã¦ãã¦ãã¾ãããã2.1 ããã¯ã¡ã½ããåãã·ã³ãã«ã§è¿ã£ã¦ããããã«ãªãã¾ããã
ãã¾ã®ã¨ããä¸çªä¾¿å©ã ã¨æã£ã¦ããã®ã¯ãprivate def
ã¨ããæ¸ãæ¹ãåºæ¥ãããã«ãªã£ãã¨ããã§ãã
大ãç®ã®ã¯ã©ã¹ãå®ç¾©ãã¦ããã¨ãã«ãé¢ä¿ãã¦ãã public ã¡ã½ãã㨠private ã¡ã½ãããè¿ãã«æ¸ããã¨ããã¨ãã¡ã½ããå®ç¾©ãããã¨ã« private :meth
ã¨æ¸ãããprivate
â å®ç¾© â public
ã¨ããå¿
è¦ãããã¾ããã
ãã®æ¸ãæ¹ã¯è¦èªæ§ãæªãããæ°æã¡ãæªãã®ã§çµå± private ã¡ã½ããã¯ã¯ã©ã¹å®ç¾©æ«å°¾ã«ã¾ã¨ãã¦ãã¾ãã®ã§ããã2.1 ããã¯ç´æçã«æ¸ããããã«ãªãã¾ããã
class A private def hoge p :private_method end end A.new.hoge #=> private method `hoge' called for #<A:0x007fc7f2a276e8> (NoMethodError)
ããããã®ãåãã¾ãããã©ããã¦ãã¡ã½ããå®ç¾©ãããããã©ä½¿ãäºå®ã¯ãªãã¨ãã«ã©ããã
class A remove_method (def hoge; end) end
ã³ã¢ã¯ã©ã¹ã®å¤æ´ç¹
Array#to_h / Enumerable#to_h
Array
ã« to_h
ã追å ããã¾ããããã¼ã¨å¤ã®é
åã®é
åã to_h
ãã㨠Hash ã«å¤æãã¦ãããããã«ãªãã¾ããã
ã¾ã Enumerable
ã«ãåçã®ã¡ã½ããã追å ããã¦ããã®ã§ Enumerable#lazy
ã§é
åãä½ã£ã¦ to_h
ã¨ããæ¸ãæ¹ãå¯è½ã§ãã
User = Struct.new(:id, :name) users = %w[John Eric].map.with_index {|name, idx| User.new(idx+1, name) } users.map {|user| [user.id, user.name] }.to_h #=> {1=>"John", 2=>"Eric"} # å¹´é½¢(age)ã¨è¡æ¶²å(blood_type)ã¨å¹´å(income)ã追å ãã¦å¤§éã®ã¦ã¼ã¶ãä½æ users.lazy. select {|user| user.blood_type == :o }. select {|user| user.age > 20 }. select {|user| user.name ~= /\AJ/ }. map {|user| [user.id, user.income] }. to_h
Binding#local_variable_xxx
ã¤ãã«ãã¼ã«ã«å¤æ°ã¾ã§å¤é¨ããæä½ã§ããããã«ãªã£ã¦ãã¾ãã¾ããã
éå»ã«ã代å
¥æã使ããã«ãã¼ã«ã«å¤æ°aãå®ç¾©ã§ããããã¨ããåé¡ããã£ã¦ãå½æã¯ãããã¯å¼æ°ã«ãã¦ä¸æ¸ãæ¹æ³ã使ã£ã¦ããããã©ããããã¯å¼æ°ããããã¯ãã¼ã«ã«ã«ãªã£ã¦ãã¾ã£ãã®ã§ä½¿ããªããªã£ã¦ãã¾ããã
2.1 ããã¯æ¥½åã§è§£æ±ºã§ããã
TOPLEVEL_BINDING.local_variable_set(:a, 5) p a #=> 5
ãã¼ã«ã«å¤æ°ã®å®ç¾©ã«ã¯ Binding#local_variable_set
ãåå¾ã«ã¯ Binding#local_variable_get
ã使ãã¾ãã
åå¨ãã¦ããªããã¼ã«ã«å¤æ°ã®å¤ãåå¾ããããã㨠NameError
ã«ãªãã®ã§ãBinding#local_variable_defined?
ã§ç¢ºèªãã¦ããåå¾ãã¾ãã
a = 0 counter = -> { a += 1 } p counter.() #=> 1 p counter.() #=> 2 p counter.() #=> 3 # ãªã»ãã counter.binding.local_variable_set(:a, 0) p counter.() #=> 1 p counter.() #=> 2 p counter.() #=> 3 # counter.binding.local_variable_get(:b) #=> NameError p counter.binding.local_variable_defined?(:a) #=> true p counter.binding.local_variable_defined?(:b) #=> false
詳ãã㯠ruby-trunk-changes r42445 - r42473 - PB memo ã® ko1:r42464 2013-08-09 18:51:00 +0900 ã®é ç®ãåç §ãã¦ãã ããã
â» 12æ9æ¥19æãã追è¨
@sora_h ããããã®ææã§ãBinding#local_variable_xxx
ã®åå¨æ義ã¯ãäºç´èªã¨è¢«ããã¼ã¯ã¼ãå¼æ°ã使ãããã¨ãã®ãã®ã ã¨ã®ãã¨ã§ãããã¯ã¦ãã®ã³ã¡ã³ãã§äºç´èªã¨è¢«ãã¯è¯ããªãã¨ã®æè¦ãããã¾ããããå人çã«ã¯ããã§å¯èªæ§ãä¸ããªãè¯ãã®ã§ã¯ãªããã¨æãã¾ãã
ãã¨ãã° class ä»ãã®span
ã¿ã°ãçæãããã«ãã¼ã¨ãã¯ã©ããã
def span(body, class:) html_class = binding.local_variable_get(:class) %q!<span class="%s">%s</span>! % [html_class, body] end puts span 'cat', class: :cute #=> <span class="cute">cat</span>
Exception#cause
ä¾å¤ããã§ã¤ã³ããã¨ãã«å ã®ä¾å¤ã辿ããããã«ãªãã¾ããã詳ãã㯠@znz ããã®è¨äºãåç §ãã¦ãã ããã
Kernel#singleton_method Module#singleton_method?
ç¹ç°ã¡ã½ããã®Methodãªãã¸ã§ã¯ããåãåºããããç¹ç°ã¡ã½ãããã©ãããåãåããã§ããããã«ãªãã¾ããã
Module#include and Module#prepend are now public methods.
include
㨠extend prepend
ã private ãããªããªãã¾ãããä½ãããããã®ãã¨è¨ãã¨ä»¥ä¸ã®ããã«æ¸ãå¿
è¦ããªããªã£ããã¨ã§ãã
A.__send__(:include, Plugin::InstanceMethods) A.__send__(:prepend, Plugin::ClassMethods)
ããããã¯ç´æçã«ããæ¸ãã¾ãã
A.include Plugin::InstanceMethods A.prepend Plugin::ClassMethods
â» 12æ9æ¥ 19æãã追è¨
@a_matsuda ããããã³ã¡ã³ãã§ææãã¦ããã ãã¦æ°ä»ããã®ã§ãããpublic ã«ãªã£ãã®ã¯ extend
ã§ã¯ãªãã2.0 ãã追å ããã prepend
ã§ããã
extend ã¯å
ã
public ã§ããã
Module#prepend
åå¨å¿ãã¦ãâ¦ã
* Module#include and Module#prepend are now public methods.
Refinements ã exmerimental ãããªããªãã¾ãã
2.0 㧠Experimental ãªæ©è½ã¨ãã¦ãªãªã¼ã¹ããã Refinements ãæ£å¼ã«çµè¾¼ã¾ãã¾ããã
refine
ã§è¨è¿°ããæ¡å¼µã using
ãå¼ãã ã³ã³ããã¹ãã«éãè¾¼ãããã¨ãã§ããæ©è½ã§ãã¢ã³ãã¼ããããå½ã¦ãã¨ãã«æ¬å½ã«å¿
è¦ãªé¨åã ãã«å½±é¿ç¯å²ãéå®ãããããªä½¿ãæ¹ãã§ãã¾ãã
Refinements ã«ã¤ãã¦è©³ãã㯠åç°ããã® Rubyist Magazine - Refinementsã¨ã¯ä½ã ã£ãã®ã ãªã©ãåç
§ãã¦ãã ããã
module Cheerful refine String do def bang "#{self}!" end end end class A using Cheerful def greet(to:) puts "Hello #{to}".bang end end A.new.greet to: 'Ruby' #=> Hello Ruby! puts 'Ruby'.bang # undefined method `bang' for "Ruby":String (NoMethodError)
ã¨ãã㧠eval
æã®ä¸ã§ refinements ãæå¹ã«ããæ©è½ã¯å
¥ããªãã£ãã®ã§ããããã
(å
¥ããªãã£ãããã§ã - shugo:r38262 2012-12-08 00:49:21 +0900 via ruby-trunk-changes r38261 - r38276 - PB memo)
String#scrub and String#scrub! verify and fix invalid byte sequence.
@sonots ããã® sonots:blog : Ruby 2.1.0 ã«è¿½å ãããä¸æ£ãªãã¤ãåãé¤å»ãã String#scrub ã®ç´¹ä» ãåç §ãã¦ãã ããã
All symbols are now frozen.
2.0 ã¾ã§ã¯ã·ã³ãã«ã«ã¤ã³ã¹ã¿ã³ã¹å¤æ°ãè¨å®ãããã¨ãã§ããããã¦ãã¾ãããããããã§ããªããªãã¾ããã
# 2.0 :a.instance_variable_set(:@a, 5) p :a.instance_variable_get(:@a) #=> 5 p :a.frozen? #=> false # 2.1 :a.instance_variable_set(:@a, 5) #=> can't modify frozen Symbol (RuntimeError) p :a.frozen? #=> true
ããããã°ã2.1-preview1 ã§ã¯ 'hoge'f
ã¿ããã«æ¸ã㨠'hoge' ã freeze ãããã·ã³ã¿ãã¯ã¹ãå
¥ã£ã¦ãã¾ããã preview2 ã§ã¯æ¶ããããã§ãã
代ãã« freeze ããã¨ãã«ç¹å¥æ±ããããããã«ãªã£ã¦ãåããªãã¸ã§ã¯ãIDãè¿ãããã«å¤æ´ããã¦ãã¾ãã
ã¾ãåæã« Hash ã®ãã¼ã«æååãæå®ããã¨ãã«ãfreeze ãããä»æ§ã«ãªã£ãããã§ãã
'a'.freeze.object_id => 70276027234660 'a'.freeze.object_id => 70276027234660
éäºæãªå¤æ´
ä»åãããã¤ãéäºæãªå¤æ´ãå ¥ã£ã¦ãã¾ããã¢ãããã¼ãã®éã«ã注æãã ããã
IO.open 㧠external encoding ã ASCII-8BIT ã®å ´å㯠internal encoding ãç¡è¦ããããã«å¤æ´
ãã¤ããªã ãã ASCII-8BIT 以å¤ãªãã§ããã¨ãããã¨ã¿ããã§ããããã¾ãã¡ç解ã§ãã¦ãã¾ããã
eval æã®ã¡ã½ãããå¼ã³åºãå ã®ã¹ã³ã¼ãæ å ±ãã³ãã¼ããããã«ãªãã¾ããã
Kernel#eval
ãKernel#instance_eval
ã Module#module_eval
㧠private
ã public
ãªã©ãå¼ã³åºãã¦ããå¼ã³åºãå
ã«ã¯å½±é¿ããªããªã£ãããã§ãã
ãã¾ãã¡åãã£ã¦ãªãã
class Foo eval "private" def foo end end Foo.private_instance_methods.grep(/foo/) #=> []
Kernel#untrusted?, untrust, and trust ã deprecated ã«ãªãã¾ãã
Kernel#tainted?, taint, untaint ã¨åãã ããã¨ã®ãã¨ã $VERBOSE ã true ã«ããã¨è¦åãã§ãããã«ãªãã¾ãã
Module#ancestors ãç¹ç°ã¯ã©ã¹ãè¿ãããã«ãªãã¾ãã
# 2.0 class A; p (class << self; self end).ancestors end #=> [Class, Module, Object, Kernel, BasicObject] # 2.1 class A; p (class << self; self end).ancestors end #=> [#<Class:A>, #<Class:Object>, #<Class:BasicObject>, Class, Module, Object, Kernel, BasicObject]
Module#define_method 㨠Object#define_singleton_method ãã·ã³ãã«ãè¿ãããã«å¤æ´
ãã¨ãã¨ã¯ã¡ã½ãããã®ãã®ãè¿ãã¦ããã®ã§ãããã·ã³ãã«ãè¿ãããã«å¤æ´ã«ãªã£ã¦ãã¾ãã
class A a = define_method(:a) { ... } a.call end
å®ç¾©ããã¡ã½ãããä¿åãã¦ããã¦ä½¿ã£ã¦ãããããªã³ã¼ãã¯ã以ä¸ã®ããã«å¤æ´ããå¿ è¦ãããã¾ãã
class A a = define_method(:a) { p :a } public_instance_method(:a).bind(A.new).call end
ãããã£ã¨è¯ãæ¸ãæ¹ãããæ°ãããâ¦ã
Numeric#quo
ã¬ã·ã¼ãã #to_r ãå®è£ ãã¦ããªãå ´å㯠TypeError ã«ãªãããã«ãªãã¾ããã to_r ãå®è£ ãã¦ããªãå ´åã¨ã¯â¦ã mathn ã«ããå½±é¿ãã©ãããã¨ãããã¨ã§ããç解ã§ãã¾ããã§ããã
ruby-trunk-changes r41110 - r41150 - PB memo
lambda proc ããã® return ã¯å¿ ã proc ããã® return ã«ãªãããã«ãªã£ã
proc ãªãã¸ã§ã¯ãã«ã¯2種é¡ãã£ã¦ã-> {}
ã lambda {}
ã§ä½ã£ã lambda proc 㨠proc {}
ã§ä½ã£ããã¡ã½ããã«æ¸¡ããããã¯ã®ãããªããããæ®éã® proc ã§ãã
lambda proc ã¯å°ãç¹æ®ã§ãå¼æ°ãã§ãã¯ããã£ãããlambda proc ã®ä¸ã§ return
ããã㨠lambda proc ãã復帰ããããã«ãªã£ã¦ãã¾ãã
ãã¨ãã lambda proc ã® return
ã¯ãlambda proc ããã® return ã«ãªãä»æ§ã ã£ãã®ã§ãããyield
ã§èµ·åããã¨ãã ããå¼ã³åºããã¡ã½ããããã® return
ã«ãªã£ã¦ããããã§ãä»åã¯ã©ã¡ãã lambda proc ããã® return
ã«ãªãããã«å¤æ´ããã¦ãã¾ãã
以ä¸ã®ã³ã¼ãã 2.0 ã§å®è¡ããã LocalJumpError
ã«ãªãã¾ããã
def a(&block) p :start yield p :end end a &lambda { return } #=> :start #=> :end
詳ãã㯠ruby-trunk-changes r42445 - r42473 - PB memo ãåç §ãã¦ãã ããã
ãããã«
Ruby 2.1.0 ã®ãªãªã¼ã¹ã¯ 12æ25æ¥ ã§ããããããå¯ç©ºã®ä¸ã§ãã¼ããªããããã« Ruby ãã³ã³ãã¤ã«ãã¦æã¾ãã¾ãããã
Ruby Advent Calendar 2013 10æ¥ç®ã®ææ¥ã¯ awakia ããã§ãã