From: jweirich@... Date: 2001-01-14T00:13:42+09:00 Subject: [ruby-talk:9246] Problems with obj.detect{} running in irb Consider the following file "tree.rb" ... ---------------------------------------------------------------------- class Tree include Enumerable attr_reader :value attr_reader :left attr_reader :right def initialize(value, left=nil, right=nil) @value, @left, @right = value, left, right end def each(&block) yield @value @left.each &block if @left @right.each &block if @right end end def createTree Tree.new(1, Tree.new(2, Tree.new(3), Tree.new(4)), Tree.new(5, Tree.new(6), Tree.new(7))) end def find(t,v) t.detect { |n| n==v } end $t = createTree $t.each {|n| print n, " "} p $t.detect { |n| n==3 } # LINE A ---------------------------------------------------------------------- I can load and run the file from irb ... bash$ irb irb(main):001:0> load 'tree.rb' 1 2 3 4 5 6 7 3 3 true But if I try to execute a line containing detect directly in irb (e.g. LINE A above), I get ... irb(main):002:0> $t.detect { |n| n==3 } LocalJumpError: break from proc-closure (irb):2:in `each' ./tree.rb:11:in `each' (irb):2:in `detect' (irb):2:in `irb_binding' Is this an irb problem, or a problem with my recursive definition of each, or a combination? -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)