Mysterious "no block given" error for an iterator

Hello,

I defined an iterator and tried using it and got a “no block given”
error. I don’t understand why I got this error because I did define a
block. The yield statement is in the replace_text function and the
iterator is called in the remove_destroy function.

Thank you,
Brian Takita

def replace_text(dir_name = ‘.’)
#raise “No Block” unless block_given?
d = Dir.new(dir_name)

        d.each{|x|
                    if x == '.' || x == '..'
                                next
                    end
                    
                    if FileTest.directory?(x)
                                puts dir_name + '--' + x
                                
                                Dir.chdir(x)
                                replace_text()
                                Dir.chdir('..')
                    else
                                if x =~ /(\.php)/
                                            puts dir_name + '->' + x
                                            f = File.new(x)
                                            
                                            content = ''
                                            f.each_line { |line|
                                                        yield line
                                                        content +=

line
}
f.close

                                            f = File.new(dir_name +

‘/’ +x, ‘w+’)
f.write(content)
f.close
end
end
}
d.close
end

def remove_destroy()
replace_text { |line|
if line =~ /.->destroy().\n/
line = ""
end
}
end

remove_destroy()

def replace_text(dir_name = '.')

[...]

                                    replace_text()

Well, you have a recursive method. For the first call the method has the
block but when you recurse you call it without the block. This is why ruby
give an error.

You must "propagate" the block, something like

def replace_text(dir_name = '.', &block)

[...]

                                    replace_text(&block)

Guy Decoux

Thank you for your help Guy.

I solved the recursion problem by replacing
replace_text()
With
replace_text() { yield }

Sincerely,
Brian Takita