MHN
Add a comment on Hacker News →
1 of 110 comments
  1. I implemented these examples in Ruby 1.9, would love to know if there are more efficient ways of doing some of these:

        def cook(p1, p2, f)
          puts "get the " + p1.to_s
          f.call(p1)
          f.call(p2)
        end
    
        cook( "lobster", "water", lambda {|x| puts "pot " + x })
        cook( "chicken", "coconut", lambda {|x| puts "boom " + x })
    
        @a = [1,2,3]
        @a.map {|x| puts x*2}
        @a.map {|x| puts x}
    
        def sum(a)
          @a.reduce(0) do |a, b|
            a + b
          end
        end
    
        def join(a)
          @a.reduce("") do |a, b|
            a.to_s + b.to_s
          end
        end
    
        puts "sum " + sum(@a).to_s
        puts "join " + join(@a)