The Artima Developer Community
Sponsored Link

Java Buzz Forum
An example Ruby patch

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
An example Ruby patch Posted: Sep 27, 2005 6:47 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: An example Ruby patch
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement
Todd Huss posted his thoughts on dealing with patches to dependencies that you rely on, in response to my Tweaking on the bleeding edge: Ruby vs. Java. Sam Ruby found an issue in Rubys XmlMarkup builder. He put up a fix for this which is very clean. I much prefer this to a diff'd file for patch to run. Isn't it a lot cleaner to see? A patch is real running code. Also, since you are able to alias :foo :saved_foo you can even do something and call that old method!
#!/usr/bin/env ruby

#--
# Portions copyright 2004 by Jim Weirich (jim.weirichhouse.org).
# Portions copyright 2005 by Sam Ruby (rubys.intertwingly.net).
# All rights reserved.

# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#++

require 'config/environment' 
require 'builder'
require 'test/unit'

class Builder::XmlMarkup < Builder::XmlBase
  def _escape(text, extra={})
    result = text.
      gsub(%r{&}, '&').
      gsub(%r{<}, '<').
      gsub(%r{>}, '>')
    extra.each_pair {|key,value| result.gsub!(key, value)}
    return result
  end

  # Insert the attributes (given in the hash).
  def _insert_attributes(attrs, order=[])
    return if attrs.nil?
    q = {'"' => '"'}
    order.each do |k|
      v = attrs[k]
      @target << %{ #{k}="#{_escape(v,q)}"} if v
    end
    attrs.each do |k, v|
      @target << %{ #{k}="#{_escape(v,q)}"} unless order.member?(k)
    end
  end
end

class TestEscaping < Test::Unit::TestCase

  def setup
    @xml = Builder::XmlMarkup.new
  end

  def test_element_gt
    @xml.title('2>1')
    assert_equal '<title>2>1</title>', @xml.target!
  end

  def test_element_amp
    @xml.title('AT&T')
    assert_equal '<title>AT&T</title>', @xml.target!
  end

  def test_element_amp2
    @xml.title('&')
    assert_equal '<title>&amp;</title>', @xml.target!
  end

  def test_attr_less
    @xml.a(:title => '2>1')
    assert_equal '<a title="2>1"/>', @xml.target!
  end

  def test_attr_amp
    @xml.a(:title => 'AT&T')
    assert_equal '<a title="AT&T"/>', @xml.target!
  end

  def test_attr_quot
    @xml.a(:title => '"x"')
    assert_equal '<a title=""x""/>', @xml.target!
  end

end

Read: An example Ruby patch

Topic: No Fluff Coming to Omaha Previous Topic   Next Topic Topic: Google Unveils Blog Search

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use