Skip to content

Instantly share code, notes, and snippets.

@podhmo
Forked from pasberth/cddr.rb
Created August 11, 2011 13:37
Show Gist options
  • Save podhmo/1139673 to your computer and use it in GitHub Desktop.
Save podhmo/1139673 to your computer and use it in GitHub Desktop.
cddddddddr
class Array
def car
first
end
def cdr
clone.tap { |r| r.delete_at 0 }
end
def method_missing(name, *args)
if name.to_s =~ /:?c(d+)r/
self.class.class_eval(<<DEFINE)
def #{name}
self[#{$1.length}..-1]
end
DEFINE
self.send(name)
else
super
end
end
end
require "test/unit"
class T < Test::Unit::TestCase
def test_car
assert_equal(0, [0,1,2,3].car)
end
def test_cdr
assert_equal([1,2,3],[0,1,2,3].cdr)
end
def test_cdddr
assert_equal([3],[0,1,2,3].cdddr)
end
end
@podhmo
Copy link
Author

podhmo commented Aug 11, 2011

ruby久しぶり。テストとメソッドのキャッシュを追加してみた。
cadadrとかやろうとするとこのままじゃ辛いけど。

@podhmo
Copy link
Author

podhmo commented Aug 11, 2011

ああ、cdrの定義そのままだった。まーいいや。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment