Xml parsing in blocks

Hi,
I was writing a small program as follows, But could not get expected
output.
Can anyone help.
--------------------------xml_document.rb-----------------------------
class XmlDocument
attr_accessor :format

def initialize(format = nil)
self.format = format
end

def method_missing(name, *args, &block)
self.class.class_eval do
define_method(name) do |*args, &block|
temp = ‘’
if args[0]
args[0].each do |k, v|
temp = temp + " #{k}=’#{v}’"
end
end

    if block
      temp = "<#{name}#{temp}>" + block.call + "</#{name}>"
    else
      temp = "<#{name}" + temp + "/>"
    end
    temp
  end
end


send(name, *args, &block)

end
end
-----------------------end of xml_document.rb---------------------------
o/p : <come_back><ok_fine
be=‘that_way’/></come_back>
expected o/p : \n \n <come_back>\n <ok_fine
be=‘that_way’/>\n </come_back>\n \n\n

@xml = XmlDocument.new(true)
@xml.hello do
@xml.goodbye do
@xml.come_back do
@xml.ok_fine(:be => “that_way”)
end
end
end.should ==
“\n” +
" \n" +
" <come_back>\n" +
" <ok_fine be=‘that_way’/>\n" +
" </come_back>\n" +
" \n" +
“\n”
end

On Mon, Mar 18, 2013 at 1:15 PM, Aashish K. [email protected]
wrote:

Hi,
I was writing a small program as follows, But could not get expected
output.
Can anyone help.

Why don’t you use Builder?

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs