なかだです。

At Mon, 8 Apr 2002 23:17:05 +0900,
Akinori MUSHA wrote:
> 今のところ、サンプルとして Dir::chdir() の拡張だけ入れてあります。
> 徐々に充実させていきましょう。

ファイルはフィーチャー単位じゃなくてクラス/モジュール単位? とり
あえずEnumerableなぞを。


Index: ruby16/Makefile =================================================================== RCS file: /cvs/ruby/src/shim/ruby16/Makefile,v retrieving revision 1.1.1.1 diff -u -2 -p -r1.1.1.1 Makefile --- ruby16/Makefile 2002/04/08 13:09:31 1.1.1.1 +++ ruby16/Makefile 2002/04/08 17:11:26 @@ -51,3 +51,3 @@ show: test: $(SETUP_COOKIE) - @cd tests; $(RUBY) -I. -I../lib test.rb + @$(RUBY) -Ctests -I. -I../lib test.rb Index: ruby16/lib/features/ruby18/enumerable.rb =================================================================== RCS file: enumerable.rb diff -N enumerable.rb --- /dev/null Thu Jan 1 09:00:00 1970 +++ enumerable.rb Tue Apr 9 02:10:47 2002 @@ -0,0 +1,33 @@ +# $Id$ + +RUBY_VERSION >= '1.7' and raise LoadError, "This module is only for ruby 1.6.x." + +module Enumerable + def all? + each {|arg| return false unless yield(arg)} + true + end + + def any? + each {|arg| return true if yield(arg)} + false + end + + def inject(result = needinit = true) + i = nil + each do |i| + if needinit + needinit = false + result = i + else + result = yield(result, i) + end + end + result + end + + def sort_by + e = i = v = 0 + collect {|e| [yield(e), (i += 1), e]}.sort.collect {|v, i, e| e} + end +end Index: ruby16/tests/features/tc_ruby18.rb =================================================================== RCS file: /cvs/ruby/src/shim/ruby16/tests/features/tc_ruby18.rb,v retrieving revision 1.1.1.1 diff -u -2 -p -r1.1.1.1 tc_ruby18.rb --- ruby16/tests/features/tc_ruby18.rb 2002/04/08 13:09:31 1.1.1.1 +++ ruby16/tests/features/tc_ruby18.rb 2002/04/08 16:39:13 @@ -3,2 +3,3 @@ require 'test/unit' require 'features/ruby18/tc_dir' +require 'features/ruby18/tc_enumerable' Index: ruby16/tests/features/ruby18/tc_enumerable.rb =================================================================== RCS file: tc_enumerable.rb diff -N tc_enumerable.rb --- /dev/null Thu Jan 1 09:00:00 1970 +++ tc_enumerable.rb Tue Apr 9 02:10:22 2002 @@ -0,0 +1,33 @@ +# $Id$ + +require 'test/unit' +begin + require 'features/ruby18/enumerable' +rescue LoadError; end + +class TC_Enumerable < Test::Unit::TestCase + def test_all? + assert_equal(true, [0, 2, 4, 6].all? {|i| i % 2 == 0}) + assert_equal(false, [0, 2, 4, 7].all? {|i| i % 2 == 0}) + assert_equal(true, ["", [], {}].all? {|i| i.empty?}) + assert_equal(false, ["", [], {}, "a"].all? {|i| i % 2 == 0}) + end + + def test_any? + assert_equal(true, [0, 2, 4, 7].any? {|i| i % 2 != 0}) + assert_equal(false, [0, 2, 4, 6].any? {|i| i % 2 != 0}) + assert_equal(false, ["", [], {}].any? {|i| !i.empty?}) + assert_equal(true, ["", [], {}, "a"].any? {|i| !i.empty?}) + end + + def test_sort_by + ary = ["BAR", "FOO", "bar", "foo"] + assert_equal(ary.sort {|a,b| a.downcase <=> b.downcase }, ary.sort_by {|a| a.downcase}) + end + + def test_inject + a = (1..4) + assert_equal(11, a.inject(1) {|x,y| x+y}) + assert_equal(10, a.inject {|x,y| x+y}) + end +end
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦