Meta

Tag: Ruby

I love that Mac OS X is really BSD under the hood, but sometimes it causes unexpected pain. More than once I’ve tripped over the differences in date between Linux and Mac OS X, and over this weekend I’ve had trauma with sed.

Specifically, if I have a single character I want to replace in a file – like replacing an out-of-band delimiter in a flat file with a TAB character – I’d normally say sed 's/\d197/\d009/g' < foo > bar. Sadly, this doesn’t work with Mac OS X’s sed.

Ruby to the rescue, in the form of my own sadsed.rb:

1

2

3

4

5

6

7

#!/usr/bin/env ruby

f=File.open$*[0]

untilf.eof?

putsf.readline.strip.gsub(/\xc7/,"\t")

end

And then just ./sadsed.rb foo > bar and away you go.

And for the record: I’m dealing with some big files, and Ruby’s Regexp is pretty snappy.

Further, let’s say you have a class Bar, derived from Foo, that redefines a couple of things:

1

2

3

4

5

6

7

classBar<Foo

THE_CONST='child_const'

def get_val

'child_val'

end

end

It seems kind of obvious that:

1

2

3

f=Foo.new

f.show_const# => parent_const

f.show_val# => parent_val

But would you have guessed that:

1

2

3

b=Bar.new

b.show_const# => parent_const WTF!?!?!

b.show_val# => child_val

This caused me no end of headaches during a weekend bout of refactoring. Pushing default constants up to the parent class completely hosed me up in those child classes that tried to override them. Overriding dumb getter methods that just return constants, though, works fine.

I’m sure there’s some computer-sciencey reason for this that Matz could explain to me, but I had really expected the every-call-is-a-message nature of Ruby to handle those two cases more consistently.