Ruby 1.8.7ã§ä½¿ããããã«ãªã£ãRuby 1.9ã®ã¡ã½ãããã¡
Ruby 1.8.7ã§ã¯Ruby 1.9ããã®backportãã¨ã¦ãå¤ããã¤ã¾ããRuby 1.9ã®ãã®ã¡ã½ãããRuby 1.8ã§ã使ããããã«ãªã£ãã¨ãããã¨ã ï¼ï¼
ããããããã¨ããæ©è½ãããã ããããã¡ãã£ã¨å¤§äººã«ãªã£ãRuby 1.8ãã楽ãã¿ã«ã
Enumeratorã¯çµã¿è¾¼ã¿ã«ãªããeachãªã©ã®ã¤ãã¬ã¼ã¿ã¡ã½ããã¯ãããã¯ãã¤ããªãã¨Enumerable::Enumeratorãè¿ãããã«ãªã£ãããããã§ãããã¯ä»ãã¡ã½ããã®æè»æ§ãé£èºçã«ã¢ããï¼
expectationsãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã«ãããã¹ãã§æ¸ãã¦ããã®ã§ãgem install expectationsããã¦ããå®è¡ãã¦ã¿ãããæ軽ã«ã¦ããããã¹ããæ¸ãããããããããæ¸å¼ã¯â¦è¦ãã°ãããããï½
ChangeLogã§ç¾å¨ããRuby 1.8.6ãªãªã¼ã¹ã¾ã§ãèªãã ã®ã§ãã»ã¨ãã©ã«ãã¼ãã¦ããã¨æããããã¤ãããâ¦
#!/usr/local/bin/ruby -wKe # -*- coding: euc-jp -*- # update (find-memofile "hatena/2008-05-08.txt") require 'rubygems' require 'expectations' # `gem install expectations' # Wed May 7 08:46:44 2008 Yukihiro Matsumoto <[email protected]> ã¾ã§ Expectations do # ãããããè¦ãããRuby 1.8.7ã ã¨ãããã¯ã«ãããã¯ã渡ãããã ãï¼ expect "block is passed" do mod = Module.new do define_method(:foo) do |&block| block.call end end extend(mod).foo { "block is passed" } # !> method redefined; discarding old expects end # Symbol#to_procã£ã¦ãããããã¼ï¼ expect [3, 3, 3, 5] do %w[foo bar baz fobar].map(&:length) end expect [1, 3] do [1,2,3,4].select(&:odd?) end # Ruby 1.9æå¼·ã®èãã¡ã½ããObject#tapãã¤ãã«ä½¿ããããã«ãªã£ããï¼ expect(:two=>2, :one=>1) do {}.tap{|h| h[:one]=1; h[:two]=2 } end # Object#instance_execãã¿ã¼ã¼ã¼ã¼ã¼ã¼ï¼ãâãï¼ã¼ã¼ã¼ã¼ã¼ã¼ï¼ï¼ expect 3 do "foo".instance_exec do length end end expect "foobar" do "foo".instance_exec("bar") do |x| self + x end end expect "FOO" do mod = Module.new do def def_each(*methods, &block) methods.each do |meth| define_method(meth) do instance_exec(meth, &block) end end end end klass = Class.new do extend mod def_each :foo, :bar do |meth| meth.to_s.upcase end end klass.new.foo end expect [5, 7] do a = 5 b = 7 instance_exec(a, b) do |x, y| [x, y] end end # Module#module_exec (class_exec) ãããããã # instance_execãinstance_evalã®é²åå½¢ã«å¯¾ãã¦ãmodule_execã¯module_evalã®é²åå½¢ã expect 8 do klass = Class.new klass.module_exec(7) do |v| define_method(:hoge) { v+1 } end klass.new.hoge end # Binding#evalã¯eval(expr, binding)ã¨ç価ã expect :in_block do bind = Object.new.instance_eval do var = :in_block binding end bind.eval("var") end # __method__ã¯ã¡ã½ããåãè¿ãçä¼¼å¤æ° expect :meth do mod = Module.new do def meth __method__ end end extend(mod).meth end # MatchData#inspectããããããããªã£ããããã§æ£è¦è¡¨ç¾ã®ãããã°ã楽ã«ãªãããã expect '#<MatchData "abc" 1:"a" 2:"b" 3:"c">' do "abc".match( /(.)(.)(.)/ ).inspect end # Method#receiverã¯ã¬ã·ã¼ã expect "recv" do "recv".method(:length).receiver end # Method#nameã¯ã¡ã½ããå expect "length" do "recv".method(:length).name end # Method#ownerã¯ã¡ã½ããã®ã¯ã©ã¹å expect String do "recv".method(:length).owner end # UnboundMethod#nameã¯ã¡ã½ããå expect "length" do String.instance_method(:length).name end # UnboundMethod#ownerã¯ã¡ã½ããã®ã¯ã©ã¹å expect String do String.instance_method(:length).owner end # GC.stress / GC.stress= ã使ããããã«ãªã£ãã # Array#flattenã®å¼æ°ã§å¹³æ»åã¬ãã«ãæå®ã§ããããã«ãªã£ãã便å©ï¼ expect [1, 2, [3]] do [1, [2, [3]]].flatten(1) end expect [1, 2, 3] do [1, [2, [3]]].flatten(2) end # Array#shuffle, Array#shuffle!ã使ããããã«ãªã£ãã expect [3,2,1,4] do srand 10 # ä¹±æ°ã®ç¨®ãæå®ãã¦å¸¸ã«åãçµæãå¾ãããã«ã [1,2,3,4].shuffle end expect [3,2,1,4] do srand 10 # ä¹±æ°ã®ç¨®ãæå®ãã¦å¸¸ã«åãçµæãå¾ãããã«ã a = [1,2,3,4] a.shuffle! a end # Array#sampleã§ã©ã³ãã ãªè¦ç´ ãå¾ãã expect 4 do srand 10 [1,2,3,4].sample end expect [4,1] do srand 10 [1,2,3,4].sample(2) end # Array#permutationã¯é åãå¾ãããããã¯ãã¤ããªãã¨Enumeratorã«ãªãã expect Enumerable::Enumerator do [1,2,3].permutation.class end expect [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] do [1,2,3].permutation.sort.to_a end expect [[1],[2],[3]] do [1,2,3].permutation(1).sort.to_a end expect [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]] do [1,2,3].permutation(2).sort.to_a end # Array#combinationã¯çµã¿åããããããã¬ãã®ããã«ç¿ã£ã確ççµ±è¨ããªã¤ãããã expect Enumerable::Enumerator do [1,2,3,4].combination(3).class end expect [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]] do [1,2,3,4].combination(3).to_a end # Array#productã¯ç´ç©éåã expect [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] do [1,2,3].product([4,5]) end expect [[1, 1], [1, 2], [2, 1], [2, 2]] do [1,2].product([1,2]) # !> method redefined; discarding old next end # Array#pop, Array#shiftã¯åãé¤ãè¦ç´ æ°ãæå®ã§ããããã«ãªã£ãã expect [3, 4] do [1,2,3,4].pop(2) end expect [1,2] do a = [1,2,3,4] a.pop(2) a end expect [1, 2] do # !> method redefined; discarding old rewind [1,2,3,4].shift(2) end expect [3,4] do a = [1,2,3,4] a.shift(2) a end # Array#index, Array#rindexã§æ¡ä»¶ã示ããããã¯ãæå®ã§ããããã«ãªã£ãã expect 1 do [0, 1, 0, 1, 0].index {|v| v > 0} end expect 3 do [0, 1, 0, 1, 0].rindex {|v| v > 0} end # Array#assocãto_aryã使ãããã«ãªã£ãã expect [1,2] do obj = Object.new def obj.to_ary() [1,2] end [obj].assoc 1 end # Enumerable#takeã¯æåã®nåãåãåºãã expect [1, 2] do [1,2,3,4].take(2) end # Enumerable#take_whileã¯æ¡ä»¶ãæºããéè¦ç´ ãåãåºãã expect [1, 2] do [1,2,3,4,5,6].take_while {|i| i < 3 } end expect [] do [1,2,3,4,5,6].take_while {|i| i > 3 } end # Enumerable#dropã¯åã®nåãåãé¤ããæ°ããé åãè¿ãã expect [3] do [1,2,3].drop(2) end # Enumerable#drop_whileã¯æ¡ä»¶ãæºããéã®è¦ç´ ãåãé¤ããé åãè¿ãã expect [3, 4, 5, 0] do [1, 2, 3, 4, 5, 0].drop_while {|i| i < 3 } end expect [1, 2, 3, 4, 5, 0] do [1, 2, 3, 4, 5, 0].drop_while {|i| i > 3 } end # take, take_while, drop, drop_while ã¯Enumerableã§ã使ããã expect [1, 2] do (1..6).take(2) end expect [1, 2] do (1..6).take_while {|i| i < 3 } end expect [3, 4, 5, 6] do (1..6).drop(2) end expect [3, 4, 5, 6] do (1..6).drop_while {|i| i < 3 } end # Enumerable#one?ã¯æ¡ä»¶ãæºãããã®ï¼çï¼ãã²ã¨ã¤ã§ããå ´åã«trueã¨ãªãã expect true do %w{ant bear cat}.one? {|word| word.length == 4} end expect false do %w{ant bear cat}.one? {|word| word.length >= 3} end expect false do [ nil, true, 99 ].one? end expect true do [ nil, true, false ].one? end # Enumerable#none?ã¯æ¡ä»¶ãæºãããã®ï¼çï¼ããªãå ´åã«trueã¨ãªãã expect true do %w{ant bear cat}.none? {|word| word.length == 5} end expect false do %w{ant bear cat}.none? {|word| word.length >= 4} end expect true do [].none? end expect true do [nil].none? end expect true do [nil,false].none? end # Enumerable#minmaxã¯æå°å¤ãæ大å¤ãåæã«å¾ãã expect [1, 6] do (1..6).minmax end # Enumerable#min_by, Enumerable#max_by, Enumerable#minmax_by ã¯ãããã¯è©ä¾¡çµæã§æå°å¤ãæ大å¤ããã¨ããã expect 22 do [18, 15, 22, 53].min_by {|x| x % 10 } end expect 18 do [18, 15, 22, 53].max_by {|x| x % 10 } end expect [22, 18] do [18, 15, 22, 53].minmax_by {|x| x % 10 } end # Enumerable#cycleã¯è¦ç´ ãã¨ã«ç¡éã«ç¹°ãè¿ãããããã¯ãã¤ããªãã¨Enumeratorã«ãªãã expect Enumerable::Enumerator do [1,2,3].cycle end # ã¡ãªã¿ã«Enumerable#takeã¯æåã®è¦ç´ nååãåºãã expect [1,2,3, 1,2,3, 1,2,3, 1] do [1,2,3].cycle.take(10) end # ç¹°ãè¿ãåæ°ãæå®ã§ããã expect Enumerable::Enumerator do [1,2,3].cycle(3) end expect [1, 2, 3, 1, 2, 3, 1, 2, 3] do [1,2,3].cycle(3).to_a end # Enumerable#find_indexã¯æ¡ä»¶ãæºããæåã®ã¤ã³ããã¯ã¹ãæ±ããã expect nil do (1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 } end expect 34 do (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } end # Enumerable#injectã§ã¤ãã«Symbolãæå®ãããã¨ãã§ããããã«ãªã£ããï¼ # åè¨ãç°¡åã«è¨è¿°ã§ããããã«ãªã£ã¦ã¦ãã¦ãã # ãããreduceã¨ããããã¾ãã«ãã³ã¤ã¤å¥åãæã«å ¥ããããMapReduceã¨å¯¾ã«ãªãããã expect 10 do (1..4).inject(:+) end expect 10 do (1..4).reduce(:+) end # Enumerable#countã¯è¦ç´ æ°ãæ¡ä»¶ãæºããè¦ç´ æ°ã®æ°ãæ°ããã # [2008/05/15] ãããã¯ã¤ãArray#nitemsã¯åé¤ãããã expect 2 do [1, 2, 4, 2, 10, 9].count(2) end expect 3 do [1, 2, 4, 2, 1, 1].count {|x| x%2 == 0} end # Enumerable#firstã expect 1 do (1..6).first end # Enumerable#group_byã¯ãããã¯ã®å¤ããã¼ã¨ããããã·ã¥ã«ã°ã«ã¼ãåãããã便å©ã expect({0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}) do (1..6).group_by {|i| i%3 } end # Enumerable::Enumerator#with_indexãããã°ã©ããªã¤ãã¬ã¼ã¿ãwith_indexçã«ãªãéæ³ã®ã¡ã½ããã expect [["abc\n", 0], ["def", 1]] do "abc\ndef".each_line.with_index.to_a end # Enumerable::Enumerator#nextã¯æ¬¡ã®è¦ç´ ãé 次å¾ãã expect [1, 2] do e = (1..6).each [e.next, e.next] end # Enumerable::Enumerator#rewindã¯æåã®è¦ç´ ã«å·»æ»ãã expect [1, 2] do e = (1..6).each e.next; e.next e.rewind [e.next, e.next] end # String#lines, String#bytesã¯Enumeratorãè¿ãããããRuby 1.9æµã ã expect ["abc\n", "def\n"] do "abc\ndef\n".lines.to_a end expect [97, 98, 99] do "abc".bytes.to_a end # String#chars, String#each_charã使ããããã«ãªã£ãã # ãã¯ãsplit(//)ãªãã¦æ¸ããªãã¦ããããªã£ãã expect ["ã", "ã¯", "ã", "ã"] do "ãã¯ãã".chars.to_a end expect ["ã", "ã¯", "ã", "ã"] do [].tap{|a| "ãã¯ãã".each_char{|c| a << c}} end # String#partition, String#rpartitionã¯ã»ãã¬ã¼ã¿åãã»ãã¬ã¼ã¿ãã»ãã¬ã¼ã¿å¾ãè¿ãã$KCODEã«å¯¾å¿ãã¦ããã expect ["ãã", "ã¯", "ãã³ã§ã"] do "ããã¯ãã³ã§ã".partition("ã¯") end expect ["123", "|", "456|789"] do "123|456|789".partition("|") end expect ["123|456", "|", "789"] do "123|456|789".rpartition("|") end # String#start_with?, String#end_with? ã¯å§ã¾ãã»çµããã®æååã®æ¤æ»ã expect true do "ããããã".start_with? "ãã" end expect true do "ããããã".end_with? "ã" end # String#slice!ã§è² ã®æ°ãæå®ãããä¾å¤ã§ã¯ãªãã¦nilãè¿ãããã«ãããsliceåæ§ã«ã expect nil do "abc".slice!(-999) end # String#index, rindexã§to_strã使ãããã«ãªã£ãã expect 3 do obj = Object.new def obj.to_str() "y" end "ruby".index(obj) end expect 3 do obj = Object.new def obj.to_str() "y" end "ruby".rindex(obj) end # String#bytesizeã¯æååã®ãã¤ãæ°ãè¿ããRuby 1.8.7ã§ã¯String#lengthã®å¥åã # Ruby 1.9ã®String#lengthã¯æåæ°ãè¿ãããã®ç§»è¡æªç½®ã expect 4 do "hoge".bytesize end # Process.execã¯execã¨åãï¼ # Kernel#loopã«ã¦StopIterationä¾å¤ãçºçãããã¨ã«ã¼ãããæããã expect :exit_from_loop do loop do raise StopIteration end :exit_from_loop end # Enumerable::Enumerator#nextã¯çµç«¯ã§StopIterationä¾å¤ãçºçãã expect StopIteration do g = [1].each g.next g.next end # ã ããEnumerable::Enumerator#nextã¨Kernel#loopã¯çµã¿åããã¦ä½¿ãã expect :exit_from_loop do g = [1].each loop do g.next end :exit_from_loop end # Range#stepã¯ç¯å²å ã®è¦ç´ ã s ããã«ç¹°ãè¿ãã expect ["a", "c", "e"] do ("a" .. "f").step(2).to_a end # Dir#inspectããããããããªã£ãã expect "#<Dir:/tmp>" do Dir.open("/tmp"){|d| d.inspect} end # Integer#odd? Integer#even? å¶å¥å¤å®ã expect true do 1.odd? end expect false do 1.even? end expect false do 12.odd? end expect true do 12.even? end # Integer#predã¯åã®æ°ãè¿ãã expect -1 do # !> ambiguous first argument; put parentheses or even spaces 0.pred end expect 10 do 11.pred end # Integer#ordã¯æåã³ã¼ããRuby 1.9ã¨ã®äºææ§ã®ããã expect 97 do 97.ord end expect 97 do ?a.ord end # Hash.[]ãto_hashã使ãããã«ãªã£ãã expect({1=>2}) do obj = Object.new def obj.to_hash() {1=>2} end Hash[obj] end # 0ã®ç´¯ä¹ expect(1.0) do 0**0.0 end expect Rational do 0**-1 end expect "Rational(1, 0)" do (0**-1).inspect end # shellwords.rbã®ã¡ã½ãã群ã require 'shellwords' expect "a\\\\x" do 'a\\x'.shellescape end expect ["ruby", "-e", "print 1"] do %!ruby -e 'print 1'!.shellsplit end expect "ruby -e print\\ 1" do ["ruby", "-e", "print 1"].shelljoin end # Tempfile.openã§æ¡å¼µåãæå®ã§ããããã«ãªã£ãããã£ãã¼ require 'tempfile' expect(/\.rb$/) do t = Tempfile.open(["temp", ".rb"]) t.path end require 'tmpdir' # Dir.mktmpdirã¯ä¸æãã£ã¬ã¯ããªãä½æããã expect(/\/foo/) do begin d = Dir.mktmpdir "foo" ensure Dir.rmdir d end end expect(/\/foo.*bar$/) do begin d = Dir.mktmpdir ["foo", "bar"] ensure Dir.rmdir d end end # Dir#each / Dir.foreach ã«ãããã¯ãã¤ããªãã¨Enumeratorãè¿ãã require 'tmpdir' expect Enumerable::Enumerator do Dir.open(Dir.tmpdir){|d| d.each.class } end expect Enumerable::Enumerator do Dir.foreach(Dir.tmpdir).class end # ObjectSpace.each_objectã«ãããã¯ãã¤ããªãã¨Enumeratorãè¿ãã expect Enumerable::Enumerator do ObjectSpace.each_object.class end # Regexp.unionã®å¼æ°ã« array of String ãåãä»ããããã«ãªã£ãã expect(/a|b/) do Regexp.union(["a","b"]) end # å®å ¨ãªä¹±æ°çºçå¨ # require 'securerandom' # SecureRandom.hex(10) # ã©ã³ãã ãª16é²æåå # SecureRandom.base64(10) # ã©ã³ãã ãªbase64æåå # SecureRandom.random_bytes(10) # ã©ã³ãã ãªãã¤ããªæåå end # >> Expectations ................................................................................................................... # >> Finished in 0.01327 seconds # >> # >> Success: 115 fulfilled
追è¨
ããã¾ãããã¾ã map.with_indexã¯ä½¿ãã¾ããã§ãããã ããwith_indexã®ãµã³ãã«ãå¤ãã¨ãã¾ããããªãã¨ããåæâ¦
追è¨
ææthxã
追è¨
id:yatmsuããã¯ã¯ã¦ãã³ã¡ã³ãã§ãæ£ç´ã1.8.xã«ã¯å ¥ãã¦æ¬²ããç¡ãã£ãããªããå人çã«ã¯ããã¨è¨ã£ã¦ããããçç±ãç¥ããããæ°å¹´ä»¥å ã«è¨ªããã§ãããRuby 1.9æ代ã«åãã¦ã®ç§»è¡æªç½®ã¨ãã¦ã¯Ruby 1.8.7ãããã¿ã¤ãã³ã°ã ã¨ä¿ºã¯æãã®ã ããä»ã®ãã¡ã«Ruby 1.9ã®ã¡ã½ãããå°å ¥ãããã¨ã§ç§»è¡æã®æéãçããã¨ãã§ããã
ç¹ã«String#linesã®å°å ¥ã¯å¤§äºã ã¨æããããããªãã¨æååãè¡ã®é åã«å¤æããæ軽ã§ãã¼ã¿ãã«ãªæ¹æ³ããªãã¦å°ãç¾½ç®ã«ãªããRuby 1.9ã®æååã¯ãã¯ãEnumerableã§ã¯ãªãã®ã ãã ã£ããæ°ããã¹ã¯ãªãããæ¸ãã¨ãã¯str.lines.mapãªã©ã¨æ¸ããããã«ãªãã¹ããRuby 1.9ã§str.mapã¨æ¸ããªãã¦ã®ã£ã¼ã£ã¦ãªãåã«ãstr.lines.mapã¨æ¸ãã¯ã»ãã¤ãããã¨ãã§ããã
ä»ã®ã¡ã½ããã¯â¦ãã£ããä½ã£ããã ããå¢ãã§å ¥ãã¡ã¾ããã£ã¦ããªãããããªãã便å©ãªã¡ã½ããã¯å¤ãã«ããããã¨ã¯ãªãã
人éã ã£ã¦ãããªã大人ã«ãªãããã§ã¯ãªãããã©ããã¦ãè伸ã³ãããããªãææ¥æããããRubyã«ææ¥æããã£ã¦ããããããªããã
追è¨
UnboundMethod#name, UnboundMethod#owner, Dir#each, Dir.foreach, ObjectSpace.each_object, Regexp#unionã«ã¤ãã¦è¿½è¨ã
追è¨
String#splitã ã¨æ¹è¡ãåãé¤ãããããããªæãã§å¾®å¦ã«æåãéãã
s = "a\nb\nc" s.lines.to_a # => ["a\n", "b\n", "c"] s.each_line.to_a # => ["a\n", "b\n", "c"] s.split(/\n/) # => ["a", "b", "c"] s.split(/(\n)/) # => ["a", "\n", "b", "\n", "c"]
追è¨
æ¥æã ä¸å «ä¸ã¯ ããè±ã
ãã¾ãï¼
追è¨
5/15 ãããã¯ã¤ãArray#nitemsã¯åé¤ãããã
[2008/05/26]追è¨
Object#instance_execã®ãã¹ãã±ã¼ã¹ã追å ã
[2008/06/01]追è¨
- Enumerable#cycleã®å¼æ°ã«ã¤ãã¦ã
- String#bytesizeã«ã¤ãã¦ã
- Enumerable::Enumerator#nextã¨Kernel#loopã«ã¤ãã¦ã
- SecureRandomã«ã¤ãã¦ã
- Module#module_execã«ã¤ãã¦ã