Archive for the ‘Uncategorized’ Category

Ruby Metaprogramming quiz #1

Implement only MyHelper module and make the tests pass. Good luck.

require 'test/unit'
 
module MyHelper
 
end
 
class Foo
  BAR = 1
  NORM = 3
  include MyHelper
end
 
class Baz
  BAR = 2
  NORM = 4
  include MyHelper
end
 
class MyTest < Test::Unit::TestCase
  def test_Foo
    foo = Foo.new
    assert_equal(1, foo.score)
    assert_equal(1, Foo.bar)
    assert_equal(1, foo.bar)
    assert_equal(3, Foo.norm)
    assert_equal(3, foo.norm)
  end
 
  def test_Baz
    baz = Baz.new
    assert_equal(2, baz.score)
    assert_equal(2, Baz.bar)
    assert_equal(2, baz.bar)
    assert_equal(4, Baz.norm)
    assert_equal(4, baz.norm)
  end
end

Tidy incantations

I have been using tidy on and off for a few years now and it’s been a great tool for finding problems with the html markup. I always wanted to use it for reformatting (cleaning up) output. Especially wanted it for reformatting xml output of some web services that have a nasty habit of putting the whole content on a single line.

But now I have found the right incantation:

 
$ cat source_file.xml | tidy -q -xml -i -w 300 > reformatted_file.xml