summaryrefslogtreecommitdiff
path: root/ruby_1_8_6/sample/list.rb
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-07 07:38:25 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-07-07 07:38:25 +0000
commit9ff1e787f915539b1980654e3d3d2013ff5c81d2 (patch)
tree8d0fc9ca5b4dbfa9885dc56862292d55091bcaac /ruby_1_8_6/sample/list.rb
parent441546edcfbb1b346c87b69c5f578d1a0e522e06 (diff)
wrong commit; sorryv1_8_6_269
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_8_6_269@17938 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ruby_1_8_6/sample/list.rb')
-rw-r--r--ruby_1_8_6/sample/list.rb80
1 files changed, 0 insertions, 80 deletions
diff --git a/ruby_1_8_6/sample/list.rb b/ruby_1_8_6/sample/list.rb
deleted file mode 100644
index 221f7edb16..0000000000
--- a/ruby_1_8_6/sample/list.rb
+++ /dev/null
@@ -1,80 +0,0 @@
-# Linked list example
-class MyElem
- # object initializer called from Class#new
- def initialize(item)
- # @variables are instance variable, no declaration needed
- @data = item
- @succ = nil
- end
-
- def data
- @data
- end
-
- def succ
- @succ
- end
-
- # the method invoked by ``obj.data = val''
- def succ=(new)
- @succ = new
- end
-end
-
-class MyList
- def add_to_list(obj)
- elt = MyElem.new(obj)
- if @head
- @tail.succ = elt
- else
- @head = elt
- end
- @tail = elt
- end
-
- def each
- elt = @head
- while elt
- yield elt
- elt = elt.succ
- end
- end
-
- # the method to convert object into string.
- # redefining this will affect print.
- def to_s
- str = "<MyList:\n";
- for elt in self
- # short form of ``str = str + elt.data.to_s + "\n"''
- str += elt.data.to_s + "\n"
- end
- str += ">"
- str
- end
-end
-
-class Point
- def initialize(x, y)
- @x = x; @y = y
- self
- end
-
- def to_s
- sprintf("%d@%d", @x, @y)
- end
-end
-
-# global variable name starts with `$'.
-$list1 = MyList.new
-$list1.add_to_list(10)
-$list1.add_to_list(20)
-$list1.add_to_list(Point.new(2, 3))
-$list1.add_to_list(Point.new(4, 5))
-$list2 = MyList.new
-$list2.add_to_list(20)
-$list2.add_to_list(Point.new(4, 5))
-$list2.add_to_list($list1)
-
-# parenthesises around method arguments can be ommitted unless ambiguous.
-print "list1:\n", $list1, "\n"
-print "list2:\n", $list2, "\n"