This post originated from an RSS feed registered with Ruby Buzz
by Premshree Pillai.
Original Post: When Code Flows Like Poetry
Feed Title: Premshree's Personal Weblog
Feed URL: http://premshree.livejournal.com/data/rss
Feed Description: Premshree's Weblog
It’s not always that you get to solve complex problems. Many a time you need to implement something—something that you know is silly enough. How do you feel? Bored, I know. However, there’s small amount of joy you get when the tool you use lets you be creative. (Of course, there’s a distinction between the larger problem at hand and the smallest implementable part of it; it is the typically joyless implementation of this small part that I’m talking about.)
I had a situation where I had to read a data file that had on each line data like thus:
foo:bar
Obviously, you want to hold all that data in a hash. Implementing this in a typical programming language would, in pseudocode, look something like thus:
data = file("data.txt").read
hsh = {}
foreach line in data do
k, v = line.split(":")
hsh["k"] = v
end
Simple enough, but boring too. With Ruby, I can play: