[Courses] [Ruby] Lesson 2: Containers, Blocks, and Iterators

Anne G anne at wjh.harvard.edu
Sun Dec 18 06:44:59 EST 2005


Questions
1. write the following string declaration code?
test = "thank you"
test2='Laurel'
test3=String.new("For")
text4="y"+"o"+"u"+"r"


2. What are the 4 things you can specify when calling a
method?
object it applies to: if not specified, applies to self.
name of method
arguments
block

* to indicate that the arguments is to be read as an array?
& associated block converted to proc object?

problem 1
----------
# Given an array of words, findLongWords returns a new array
# containing the words with more than 5 letters.

def findLongWords(words)
  longWords = Array.new()
  words.each { |wrd|  longWords.push(wrd) if (wrd.length>5)
}
  return longWords
end
p findLongWords(['thisOneIsLong', 'short', 'howAboutThisOne'
])

solution with enumerable

def findLongWords(words)
  words.find_all {|wrd| wrd.length>5}
end
p findLongWords(['thisOneIsLong', 'short', 'howAboutThisOne'
])

problem2
---------
arr=%w{ dog cat aardvark bird}
puts "simple sort"
puts arr.sort
puts "new sort"
puts arr.sort_by{|wrd| (wrd.count("aeiou") + 0.01*wrd[0])}

questions: yield and blocks?
-----------------------------------
the simple block formulation can be understood as is, but I
don't really get yield, and blocks...

Someone gave the following example on comp.lang.ruby

class List
   def add(obj)
     @first = [@first,obj]
   end

   def each
     e = @first
     while e
       yield e[1]
       e = e[0]
     end
   end
end

l = List.new
l.add("hello")
l.add("world")
l.add("of")
l.add("beauty")

l.each do |s|
   puts s
end

It puts the words in reverse order, but I don't understand
why e[1] turns out to be the last word in the
sequence? and why the sequence shortens each times
it goes through the while loop.

Since I don't understand the code, I haven't really
figured out what the yield does, why it is interesting

------------------------------------------------------------
Here are the other examples for those interested in yield
that I would like to study next to try to get the point of
yield.

Example B): Traversing a data structure recursively:

class Array
   def traverse_rec
     for e in self do
       if e.kind_of? Array
         e.traverse_rec
       else
         yield e
       end
     end
   end
end

[ [1,2] [1,3,[4,[5] ],2,[3,4] ] ].traverse_rec do |el|
    puts el;
end

outputs all elementes of the tree recursively.
(The tree is implemented as an array of (possible arrays of
  (possible ....))... )

------------------------------------------------------------
Example C)

Do something with each element of the a data structure
(minor variation of example 1):

class List
   def add(obj)
     @first = [@first,obj]
   end

   def map
     e = @first
     while e
       e[1] = yield e[1]
     end
   end
end

l = List.new

l.add("hello")
l.add("world")

l.map do |s|
   s.length
end

Each element of the list is replaced by its length.

-------------------------------------------------------------
Example D)

Do something according to a dynamic criterion

(Sum up all elements of a list conditionally)

class List
   def add(*objs)
     objs.each do |obj|
        @first = [@first,obj]
     end
   end

   def sum_if
     e = @first
     sum = 0;
     while e
       sum += e[1] if yield e[1]
     end
   end
end

l = List.new
l.add(1,3,4,5,6,2,1,6);

l.add_if do |i|
   (i%3)==0
end

The last function sums all elements of the list which
are divisible by 3.

I hope it gives you some insides about the power
of yield.
Regards, Christian







More information about the Courses mailing list