Skip to content

Ramblings

Musings of Matt Williams
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'refactoring' Category
11Sep08 universal cat redux
programming refactoring ruby utilities
2 Comments

I have a neglectful relationship with inject. That is, I neglect to remember that it exists, having worked for so long with other languages which are “unfamiliar with the concept”. Amos King’s blog entry on Inject & Me – BFFs got me to thinking that ucat (see cat on steroids (or cat on a hot ruby roof)) could use inject as opposed to the each_byte. So, instead of

def non_printing(line)
  proc = ""
  line.each_byte do |c|
    proc += case c
      when (0 .. 8): "^#{(c + 64).chr}"
      when (10 .. 11): "^#{(c + 64).chr}"
      when (13 .. 26): "^#{(c + 64).chr}"
      when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
      when 127: "^?"
      when ((c & 128) == 128): "M-0#{c.to_s(8)}"
      else c.chr
    end
  end
  proc
end

I can do:

def non_printing(line)
  line.split("").map{|c|c[0]}.inject("") do |s,c|
    s += case c
      when (0 .. 8): "^#{(c + 64).chr}"
      when (10 .. 11): "^#{(c + 64).chr}"
      when (13 .. 26): "^#{(c + 64).chr}"
      when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
      when 127: "^?"
      when ((c & 128) == 128): "M-0#{c.to_s(8)}"
      else c.chr
    end
  end
end

I still think there needs to be a better way — going from string to an array of strings mapped to an array of bytes so that I can process it via inject seems to be awkward. So, I do some searching and find Object#enum_for (let me plug gotAPI — it’s a great tool for searching a large number of API’s) and come up with:

  def non_printing(line)
    line.enum_for(:each_byte).inject("") do |s,c|
      s += case c
              when (0 .. 8): "^#{(c + 64).chr}"
              when (10 .. 11): "^#{(c + 64).chr}"
              when (13 .. 26): "^#{(c + 64).chr}"
              when (27 .. 31): "^#{%w([ \\ ] ^ _)[c - 27]}"
              when 127: "^?"
              when ((c & 128) == 128): "M-0#{c.to_s(8)}"
              else c.chr
           end
    end
  end

That seems cleaner to me. One of the things I love about ruby is that there’s usually more way than one to do something. And it’s often quicker, like in Unix, to go with what you know rather than making it more elegant. However, I also like that it’s easy to write elegant code.

And elegant code is a thing of beauty.

You can download the updated version of ucat.rb (you may need to rename it to ucat.rb)

 
Browse Archives ยป
  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • cygwin (1)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (19)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (6)
  • jruby (8)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (7)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (10)
  • tools (2)
  • torquebox (2)
  • Uncategorized (9)
  • UNIX (1)
  • utilities (3)
  • web (5)
  • web design (3)
 

Latest

  • cygwin and torquebox and rvm, oh my!
  • JBoss Client Jars for Messaging
  • rsh hates nohup
  • Torquebox and Cygwin: Take I
  • Rails & JRuby in a Jar
  • Fractal Terrain Generation
  • Quick thought on programming and distractions
  • Using jnp as a JBoss heartbeat
  • z-index and events
  • JBoss port confusion

Flickr

layout_newm3headerTerrain Testa

Blogroll

  • Development Blog
  • Documentation
  • Plugins
  • Suggest Ideas
  • Support Forum
  • Themes
  • WordPress Planet

Search

Browse by Category

  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • cygwin (1)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (19)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (6)
  • jruby (8)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (7)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (10)
  • tools (2)
  • torquebox (2)
  • Uncategorized (9)
  • UNIX (1)
  • utilities (3)
  • web (5)
  • web design (3)

Browse by Tag

  • 1.2.6
  • 2.1
  • administrivia
  • autotest
  • books
  • controller
  • css
  • feed-normalizer
  • feeds
  • gotchas
  • idiocy
  • irb
  • Java
  • javascript
  • jboss
  • jruby
  • just enough programming
  • mini sagas
  • open-uri
  • philosophy
  • php
  • pragmatism
  • programming
  • quotations
  • rails
  • rants
  • reading
  • restful_authentication
  • rspec
  • rss
  • ruby
  • rubygems
  • scriptaculous
  • setup
  • simplicity
  • sprites
  • statemachine
  • tips
  • treetop
  • utilities
  • web
  • web design
  • websense
  • yaml
  • zentest

Browse by Month

  • June 2012 (1)
  • November 2010 (1)
  • August 2010 (1)
  • June 2010 (1)
  • March 2010 (1)
  • September 2009 (1)
  • August 2009 (2)
  • July 2009 (2)
  • May 2009 (1)
  • April 2009 (1)
  • February 2009 (4)
  • January 2009 (2)
  • December 2008 (2)
  • November 2008 (5)
  • October 2008 (3)
  • September 2008 (12)
  • August 2008 (28)
 
 
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed