Ruby’s Object has a method, methods. You can use it to see the methods which an object has. Sort of. In this post I’m examining methods, public_methods, and private_methods as well as some of their implications.
Continue reading ‘methods, public_methods, and private_methods’
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)
I got to thinking about SuperIO and how it could be used as a swiss army chainsaw to open files, whereever they might be on the net. From there, my fevered mind got to thinking about cat and how the two could be used together. That said, I present ucat — a universal cat, if you will, which does not need to be herded, but rather will do as you ask. It’s expecting to be able to find SuperIO, so you’ll need to make it available.
Continue reading ‘cat on steroids (or cat on a hot ruby roof)’
Quotes can provide inspiration or food for thought. In this entry, I’m examining the use of open-uri and feed-normalizer to get a random quote which could be used as either a thought for the day or a “fortune” replacement.
In this blog entry, dear reader, we examine the statemachine and treetop gems via an old standby, a Zork imitation. And, despite the title, you won’t find a grue.
$ ruby adventure.rb This is the beginning. Like all tales, there's a beginning, a middle and an end.... Paths lead n. What do you want to do? n This is a path in the forest, it looks fairly well travelled. You see a clearing to the east Paths lead w, n, e, s. What do you want to do? e This is a clearing. You can actually see the sky here. Compared to the deep dark forest, it seems a relief. A path can be seen to the west. Paths lead w, n, e, s. You see the following: letter. What do you want to do? get letter Ok, you pick up the letter. What do you want to do? w This is a path in the forest, it looks fairly well travelled. You see a clearing to the east Paths lead w, n, e, s. What do you want to do? w You're lost in the depths of the forest. You're not sure where you are, nor how to get out of there. Paths lead w, n, e, s. What do you want to do? quit
Continue reading ‘A “grue”some look at Statemachine and Treetop’
Yesterday, I posted an article entitled Break my code, please, wherein I posted a very fragile piece of code, with the challenge to find ways in which to break it.
What follows is a discussion of the code and why it is bad/fragile/easily broken…..
Continue reading ‘Why my code is broken….. (break my code redux)’
There’s a number of things we can should take into account when writing code — boundary cases, etc., that can make or break a programme. Yes, testing is important, but I think that developing good defensive programming practices is even more important. That said; I present the following, fragile, code. Just about every line has a case where it can fail. Can you break my code? I’ll post some test cases which cause it to fail later.
And for those looking for the next installment of O_RLY?, it’s coming soon; I’ve been embroiled in a hideous move.
One of the (imho) lesser used pieces of the Ruby language is ObjectSpace. In this article I’ll show one of the things you can do with it — get all the subclasses of a class.
Continue reading ‘Ruby’s ObjectSpace: Subclasses’
autotest / zentest are really useful tools. However, it’s important to remember to run migrations for the test database — otherwise your tests will fail (miserably!)
I’ve found the following to be helpful for using autotest:
Rubygems, those magickal behind-the-sceneslibraries we all use without thinking too much about them deserve some love. I’m singing the praises of 7 rubygems out of those installed on my laptop (this isn’t to say that I love them more than others). What are some of y’all’s favorite gems?
Continue reading ’7 Ruby tools which are gems and why I like them’
- administrivia (6)
- books (1)
- Computers (2)
- css (3)
- eating crow (1)
- games (1)
- glassfish (1)
- gotchas (17)
- howto (2)
- idiocy (3)
- javascript (4)
- jboss (5)
- jruby (7)
- 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 (9)
- tools (2)
- torquebox (1)
- Uncategorized (9)
- utilities (3)
- web (5)
- web design (3)



