Ruby

RubyConf 2006

So, last weekend I went to RubyConf 2006. And yes I am finally getting around to writing about it… and this probably won’t be the last time. It was a very interesting experience for me, but I have not quite got my mind around all that happened there yet.

It was immensely fun, and I definitely learned alot. It was great catching up with so many people that I have met over the last few years. And of course, how can you not have fun hacking on code into the wee hours of the night? (even though a little bit of it was *gasp* Java! *gasp*)

If you have not seen the blow-by-blow, take a look at Nick Seiger’s blog. Also, there are a ton more links out there:

A great one-act/one-man play by Adam Keys at RejectConf (which I unfortunately missed… too busy writing code and trying to find coffee :) ).

Tim Bray has a recap and some comments on his talk.

Curt Hibbs aggregates it all

Also, Ruby-lang chimes in (with some more linkage).

General
Rails
Ruby
Self
Software Development

Comments (0)

Permalink

A Ruby DSL for splitting files

Tonight Blaine sent me his version of my Simple Ruby Log splitter, that took it a giant step further and made a reuseable library to split files. He used an object oriented approach, as well as provided extensibility by allowing the caller to pass in a block that determined when to split the file. Hopefully he will post the code either on my site or his. ;) Good stuff.

Anyway, he brought that problem back into my head, and I immediately thought about making a DSL for splitting files. The code I came up with is below.
Note: I am hoping to share ideas here. If anyone has a better way to do this please please please either post your thoughts/code in the comments or blog about it and send me the link. I am still very much learning Ruby, and would love to see what other people are doing.

  1. #
  2. # read from $stdin, splitting it into multiple files, using a DSL of sorts
  3. #
  4. def nextFile(filename, count)
  5.         File.open(“#{count}_#{filename}”, “w”)
  6. end
  7.  
  8. def split(instream, filename, args)
  9.         currentFile = nil
  10.         lines = count = 0
  11.         lineBreak = args[:lines].to_i
  12.         instream.each do |line|
  13.                 currentFile = nextFile(filename, count+=1) if (lines%lineBreak) == 0
  14.                 lines += 1
  15.                 currentFile.write line
  16.         end     
  17. end
  18.  
  19. def parse(command)
  20.         command.gsub(/into|when/, “,”).gsub(/=/,“=>”)  # overly simplistic, no error handling, etc
  21. end
  22.  
  23. def run(command)
  24.         eval parse(command)
  25. end
  26.  
  27. run “split $stdin into ’split.log’ when :lines = 1000″

Of course, one quick look at this will show that this example is simplistic, and that there is much work that could be done to make it more robust and/or full featured. That is not the point. The point is line 27, where your business-tier developers only need to type in near-english syntax to get their jobs done. The rest of it is not too hard in this case… though it can get much much harder in other cases. However, that can be handled by a relatively few developers, and greatly simplifies the work for the rest.

Anyone care to take this example further?

p.s.> I know there is some work being done on DSL books for Ruby. I cannot wait until I get my hands on one. If anyone out there needs a reviewer, drop me a line. ;)

General
Ruby
Thought

Comments (2)

Permalink

Simple Ruby log splitter

The other day I needed to split up a large log file into smaller, more manageable bits. Long ago I wrote a Java based file splitter, and long before that I wrote one in C++ (woohoo iostreams!). But unfortunately, I did not have either of these programs with me.

So, I wrote it in Ruby:

  1. #
  2. # read from $stdin, splitting it into multiple files, based off of MAX_LINES
  3. #
  4.  
  5. FILE_SUFFIX = ARGV[0] ? ARGV[0] : “split.log”
  6. MAX_LINES = ARGV[1] ? ARGV[1].to_i : 500000
  7. lines = 0
  8. fileCount = 0
  9. currentFile = nil
  10.  
  11. $stdin.each do |line|
  12.         if (currentFile == nil)
  13.           fileName = “#{fileCount}_#{FILE_SUFFIX}”
  14.           puts “Writing file “ + fileName
  15.           currentFile = File.open(fileName, “w”)
  16.           fileCount+=1
  17.           lines = 0
  18.         end
  19.  
  20.         currentFile.write line
  21.         lines+=1
  22.  
  23.         if (lines > MAX_LINES)
  24.           currentFile.close
  25.           currentFile = nil
  26.         end
  27. end

Usages:

ruby split.rb < file

ruby split.rb mylogfilename < file

ruby split.rb mylogfilename maxlines < file

This is by far the smallest (and simplest) I have ever gotten the file splitter code, without using extra jars in Java. There are a number of reasons for this, not least of which is the elegant use of closures in Ruby to pass each line, instead of having to do all of the reading directly in my code.

But really, I’m putting this out here to see what ideas or approaches other people might have on how I could have done this better/more efficient, etc. So, please post your thoughts and comments. I would love to see a more elegant approach.

General
Ruby
Software Development

Comments (5)

Permalink

Sexy as hell irb (Interactive Ruby)

At OSCON, I had the pleasure of meeting John Lam, principal at ObjectSharp, creator of the Ruby CLR bridge for .Net, owner of a blog with a kick-ass name (if you are a reformed COM developer), and an all around great guy. One of the things he was showing off on the RubyCLR/.Net platform was this Avalon-based irb session that was out-of-this-world sexy. Here it is:

sexy irb

This is almost exactly what I have been looking for in an editor: something that enhances my focus on the code. All too often I get distracted by some peripheral event on my desktop: a system update, an im chat window, the alluring icon to Firefox… I want to immerse myself in the code, and this looks like the perfect environment for that. Now, if it only ran on Ubuntu, :-)

General
Ruby
Software Development

Comments (3)

Permalink

RubyConf registration is open!

Get your registration on here: RubyConf.com

It will be interesting to see how long it lasts.

Update: Apparently it lasts about 1 day! Just checked the site and they are sold out, but are accepting people on the waiting list.

General
Rails
Ruby

Comments (0)

Permalink