From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/sample/list.rb | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 jni/ruby/sample/list.rb (limited to 'jni/ruby/sample/list.rb') diff --git a/jni/ruby/sample/list.rb b/jni/ruby/sample/list.rb new file mode 100644 index 0000000..85899ce --- /dev/null +++ b/jni/ruby/sample/list.rb @@ -0,0 +1,81 @@ +# 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 + @head = 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 = "