Your Ad Here

Tag Archive for 'ruby'

Stupid Ruby Tricks

I can remember, once, long ago, being entranced with the idea of writing programs whose output was identical to its own code. This is, of course, entirely impractical and not really good for anything.

Ruby has a built-in variable, $0, which refers to the name of ruby program currently being executed, and which therefore makes the writing of self-describing programs trivial. So, you can write a ruby program which outputs itself like so:

1
2
#!/usr/bin/ruby
IO::readlines($0).each{|n|puts n}

Pretty nifty. Can it get any shorter? If you’re willing to use bash commands, yes it can:

1
2
#!/usr/bin/ruby
system("cat #{$0}")

If you want to dispense with the shebang and run it with ruby stupid.rb (or whatever you’d like to title your work of art) then you can shave another 15 characters off of it. The shortest way I could see to get it would be

1
puts `cat #{$0}`

which assumes you have to run it with ruby ... rather than just with it’s own name. That’s all rather pointless, of course.

Of course, the $0 variable also means that you can do exciting things like rewrite the file which is currently executing while you’re executing it:

1
2
#!/usr/bin/ruby
File::open($0,mode="w").write('puts "Hello #{`whoami`}"')

Have fun with that. Oh, and, um… only try this at home.

Some Ruby & Rails Tricks

I certainly can’t claim to have discovered these tips; but they’ve been pretty helpful to me. Maybe this will just make them a little easier to find for whoever is looking for them next.

Getting A Collection’s Index From Within a Partial

Lets say you’re passing a collection to a partial, but that somewhere in that partial you are interested in knowing which index you’re at. Do you need to implement a counter? Nope; there already is one. A collection passed to a partial implicitly creates a counter called [partial_name]_counter. So, if you called with

1
< %= render :partial => 'thing', :collection => @a_few_of_my_favorite_things %>

then in your partial, you could use:

1
< %= thing_counter %>

to return the index as you plow through the collection. Life is good. Rails is looking out for you. (Hat tip to GMarik, which is where I saw this one.)

Obtaining a Class name From a String

Today I found myself wanting to take a string, let’s say "Thing", and use it as a class name, Thing. Again, it turns out this can be done. In Ruby, class names are constants, so you can do

1
2
str = "Thing" #=> "Thing"
Kernel.const_get(str) #=> Thing

Hooray!

Make Finding Easier

Find yourself writing long, complicated find expressions in your code? There is a better way.

Perhaps in your code… maybe even in a view… maybe you want to find all Things that don’t belong to the current use, and which are flagged with some property, and you’re temped to write something like:

1
Thing.find(:all, :conditions => ["user != ? and flag = true", current_user])

What we could do is put some has_finders in the Thing model, something like

1
2
3
class Thing < ActiveRecord::Base
has_finder :exclude_user, lambda {|user| {:conditions => "things.user_id != #{user.id}"}
has_finder :with_flag, :conditions => "things.flag = true"

Having done that, you could now replace that first bit with:

1
Thing.find(:all).with_flag.exclude_user(current_user)

It’s a little shorter, sure, but more importantly, when you read it, it just makes more sense. A completely new person coming to look at this line would have a good idea what it was doing without even looking in the model to see what the has_finders were doing.

Okay then

If you’ve spent any amount of time using rails, these probably aren’t new to you. Everything’s new at least once, though, so hopefully these are helpful to someone.

Other favorite ruby or rails tricks? Leave a comment! Heck, we’re not prejudiced, here, go ahead and comment with your favorite Python or Perl or [other language] tricks, too, if you like.

Flex and Rails

I’ve started to be interested in Flex, recently. This interest piqued when I saw some of the rates that were being offered for Flex developers in this area, so I suppose my interest is completely mercenary and not particularly motivated by an interest in the technology, per se. I think that I’m okay with that.

That said, it does look interesting. And, naturally, some people are using Flex front-ends for Rails applications: one good resource in particular looks to be http://flexonrails.net/.

Also interesting is Project Sprouts, which is a gem and seems to make good use of ruby and rake to automate some aspects of ActionScript, AIR, and Flex development. I’ve had no time (yet) to give it a test drive, so this is nothing more than a pointer: if it sounds interesting, give it a look.

Since I’m thinking about ActionScript, this is as good a time as any to revisit the venerable ActionScript Jabberwocky. Enjoy.

The ActionScript Jabberwocky, from http://www.turdhead.com/2004/08/16/the-actionscript-jabberwocky/

The ActionScript Jabberwocky, from http://www.turdhead.com/2004/08/16/the-actionscript-jabberwocky/

Joyent has JRuby

So, Joyent has JRuby support. That is all.