hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Thursday

2005.04.28

Ishii-san died #

by daigo in cult

Masaru Ishii, a great Japanese Rubyist, died at the rail disaster, where 106 people died. He introduced RubyUnit with the author1, Suketa-san. It was the predecessor of the current Test::Unit in Ruby 1.8.

I love unit testing. I pay his last respects. I don’t know what to say in English. I hope I don’t use incorrect words. I learned RubyUnit from Suketa-san’s book.

1 According to this blog, Ishii-san asked Suketa-san to create a Ruby implementation of xUnit.

Sparklines for Minimalists #

by why in inspect

If you’ve read Joe Gregorio’s Sparklines in data: URIs in Python, then you know that he’s presented us with an incredibly compelling use for data: URIs. Namely, this is not an external file: But we don’t have Python’s image library, so what are we to do?

What if I offered you a handful of code that could generate inline sparkgraphs without library code? Presenting Bumpspark.

 def bumpspark( results )
     white, red, grey = [0xFF,0xFF,0xFF], [0,0,0xFF], [0x99,0x99,0x99]
     ibmp = results.inject([]) do |ary, r|
         ary << [white]*15 << [white]*15
         ary.last[r/9,4] = [(r > 50 and red or grey)]*4
         ary
     end.transpose.map do |px|
         px.flatten!.pack("C#{px.length}x#{px.length%4}")
     end.join
     ["BM", ibmp.length + 54, 0, 0, 54, 40, 
       results.length * 2, 15, 1, 24, 0, 0, 0, 0, 0, 0].
       pack("A2ISSIIiiSSIIiiII") + ibmp
 end

It’s got one known bug: graphs built from 16-element arrays get staticky. Free inky duck drawing to first patcher. (Keep reading.)

Great coats! jzp has dropped a ping version in the comments! MenTaL has a bump one with compression! Peewee graphics libs within!

How Do You Parse Tab Separated Values #

by daigo in bits

How to parse tab separated values, values of which may be omitted and they should be recognized as nil—that was discussed at ruby-list.

For instance each row has three values: float, integer and string; “1\t2\t3” should be 1.0, 2, “3”. When values are omitted they should be parsed as nil, not zero nor empty string; “1\t\t3” should be 1.0, nil, “3”.

Following is a solution of Rubikichi-san1.


class TextData < Struct.new(:float, :int, :str)
  CONVERTER = [:to_f, :to_i, :to_s]
  def self.[](input)
    obj = new
    input.chomp.split(/\t/).each_with_index do |x, i|
      obj[i] = (x && x.__send__(CONVERTER[i]))
    end
    obj
  end
end

p TextData["1\t2\t3"]
p TextData["1.0\t2\t"] 
p TextData["1\t2\tfoo"] 

# <struct TextData float=1.0, int=2, str="3">
# <struct TextData float=1.0, int=2, str=nil>
# <struct TextData float=1.0, int=2, str="foo">

Inherit an anonymous structure class!

1 He is a pioneer of Ruby. I learned Ruby techniques from his book and home page. “Rubikichi” is his alias.

There is extra functionality in the original question, which I skip here; converting the string value is required.