Category Archives: ruby

More Ruby stuff

Poignant guide, http://ww.poignantguide.net . Possibly the weirdest introduction to a programming language that I will ever see in my life . – Dave

Also, read Why the Lucky Stiff.

On Technorati:

Nifty Japanese stuff: Kakasi

Kakasi is an external utility for
converting Japanese text between coding systems. It can also add
furigana after kanji or convert a text file to romaji.

Debian users can apt-get install kakasi kakasi-dic.
There’s an Emacs interface,
a Perl module (Text::Kakasi),
and a Ruby library.

On Technorati: , , ,

Adventures with Ruby

This is my second day of Ruby, a programming language that is rather
popular in Japan. I’m in love. It’s now my second-favorite programming
language. (Emacs Lisp is, of course, the first.)

Yesterday’s script parsed schedule data and checked that monthly cost
and day constraints were observed. Today I wanted to visualize the verified schedule.

At first I tried working with planner (formerly known as
MrProject and not to be confused with PlannerMode). I wrote a Ruby program that converted my schedule.csv into XML, and planner loaded it successfully. However, I didn’t think planner would let me do funky color coding. I thought about using etask, but ended up deciding to write something using libgd-ruby.

It was surprisingly easy to write a Gantt-like visualizer for the schedule
and even easier to manipulate it once I had written it. For example, I could do silly things like

s.to_image((s.schedule.sort { |a,b| a.start <=> b.start }, 0, 2500, 1800, image)

Sorted by start date

to see the tasks sorted by start date. Being a Lisp girl, I had no problems writing silly things like

s.to_image((s.schedule.collect { |x| x if x.person_id == ’08-1′ } – [ nil ]).sort { |a,b| a.start <=> b.start }, 0, 2500, 1800, image)

but then I realized that this was much cleaner:

s.to_image((s.schedule.sort { |a,b| x = a.person_id <=> b.person_id; if x == 0 then a.start <=> b.start else x end }, 0, 2500, 1800, image)

Sorted by person and then start date

Ruby is so cute!

On Technorati:

Ruby: Turn bash.org quotes into a fortune file

The following code turns XML quotes from bash.org (a popular IRC quotes server) into a fortune-cookie file.
Handy for using with ../emacs/flashcard.el and my ../emacs/flashcard-config.el, which pops up a fortune
every time I get a correct answer.

bash-org-to-fortune.rb:

require 'rss/1.0'
require 'cgi'
require 'net/http'
host = Net::HTTP.new('bash.org', 80)
if ARGV[0] then
   resp, data = host.get('http://bash.org/xml/?top&below=' + ARGV[0], nil)
else
   resp, data = host.get('http://bash.org/xml/?top', nil)
end
parsed = RSS::Parser.parse(data, false)
parsed.items.each { |x| puts CGI::unescapeHTML(x.description.gsub('
', "\n")); puts "%\n" }

Call like this:

ruby bash-org-to-fortune.rb > bash; strfile bash; fortune bash

# or to get the top quotes with score < 1000
ruby bash-org-to-fortune.rb 1000 > bash; strfile bash; fortune bash

On Technorati: , ,

The Year in Bookmarks

Top 10 tags for 2005 productivity(104) web2.0(88) digitalpinay(88) social(84) useful(83) business(80) blogs(70) research(69) lifehacks(68) blogging(60)

Check out my year in bookmarks for more detail. =) If you want me to analyze yours, just save http://del.icio.us/api/posts/all to all.xml and run this Ruby script. You can also e-mail me ([email protected]) your all.xml if you don’t want to go through the hassle yourself.

Much fun!

#!/usr/bin/ruby

require 'rexml/document'
require 'date'

include REXML

YEAR = 2005
USER = "sachac"
doc = Document::new(File::new('all.xml'))

month_hash = {}
month_total = {}
tag_total = {}
doc.elements[1].elements.each {
  |x|
  date = DateTime::parse(x.attributes['time'])
  if (date.year == YEAR)
    x.attributes['tag'].split(' ').each {
      |tag|
      month_hash[date.month] ||= {}
      month_hash[date.month][tag] ||= 0
      month_hash[date.month][tag] += 1
      tag_total[tag] ||= 0
      tag_total[tag] += 1
    }
    month_total[date.month] ||= 0
    month_total[date.month] += 1
  end
}

s = "Top 10 tags for " + YEAR.to_s + " |"
tag_total.sort_by { |tag,total| -total }.slice(0, 10).each { |tag,total|
  s += ' " + tag + "(" + total.to_s + ")"
}
puts s

month_hash.sort.each { |month,tags|
  s = Date::MONTHNAMES[month] + "
(" + month_total[month].to_s + " bookmarks) |" tags.sort_by { |tag,total| -total }.each { |tag,total| s += ' " + tag + "(" + total.to_s + ")" } puts s }

On Technorati: , ,

Girl!

I play around with Ruby on Rails, and what do I do?

Build one webapp to keep track of what’s in my fridge, and another
webapp to keep track of what’s in my closet.

I’m going to do it in _pink_, too… ;)

On Technorati: , ,

Random Japanese sentence: 猫は鳥めがけて飛び跳ねた。 The cat sprang at the bird.