Created
December 30, 2010 20:03
-
-
Save yuya-takeyama/760217 to your computer and use it in GitHub Desktop.
ArrayLike object in Ruby 1.9.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArrayLike | |
include Enumerable | |
def initialize(arr) | |
@arr = arr | |
end | |
def count | |
@arr.count | |
end | |
def each | |
@arr.each do |value| | |
yield value | |
end | |
end | |
def [](key) | |
@arr[key] | |
end | |
def []=(key, value) | |
@arr[key] = value | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'arraylike' | |
arr_obj = ArrayLike.new ['foo', 'bar', 'baz'] | |
puts arr_obj.count | |
arr_obj.each_with_index do |value, key| | |
puts "#{key}:#{value}" | |
end | |
arr_obj[1] = 'hoge' | |
puts arr_obj[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment