Archive for the 'ruby' Category

Ruby Development Woes

01/08/2011

After getting back into the flow of things post vacation I decided it was about time to put something up on my blog. A few days ago one of the Debian ruby package maintainers quit with a post detailing what he found to be some current problems with the Ruby community. As someone who works mostly in Ruby I share some of his opinions, albeit for different reasons.

To me the biggest problem with the Ruby development community is also its greatest strength. In the Ruby ecosystem nearly everything is optimized for the developer. What do I mean by this? Let’s look at it from the example of a couple of of the conclusions reached in the above-mentioned post.

The core Ruby development community should mature

One of my favorite things about being a Ruby programmer is how fast it is to code, package and distribute a library. Seriously, look at the front page of rubygems.org:

gem update --system
gem build foo.gemspec
gem push foo-1.0.0.gem

Just run these commands and you have released your code and anyone else can easily search for and install it on their system.

However, I think this process is often too easy. It seems very frequent that versions of gems are pushed before they are completely finished resulting in quick second versions release immediately after the first. Even worse, since a developer can share their project so easily version are often pushed in a “good enough” state and never reach major releases. Rake &emdash; the standard for Ruby project build scripts; used in pretty much any Ruby project you’ll come across &emdash; is still on version 0.8.7!? (That’s not to say that Rake isn’t stable software, I’m just pointing out how common it is for a Ruby developer to use libraries and tools that haven’t even reached their first major release and not even think about it).

I’m guilty of this myself. Pretty much everything that I’ve pushed out to rubygems was nowhere near complete. I just pushed it out because it was the easiest way to share it with my friends/co-workers and it was “good enough” for our purposes.

In addition to this, it is very difficult to judge the quality of any library you will see on rubygems. Does this gem being version “0.1.0” mean that it is incomplete, or are they just judicious in their versioning? This isn’t a problem unique to Ruby, but the easy of releasing Ruby packages certainly exacerbates the issue.

The Ruby community should acknowledge that RVM and Rubygems are not for everybody

RVM and Rubygems make my life much easier. I can search for gems to help me write applications I’m working on, isolate them from the rest of my system, and test against multiple versions or Ruby very easily. However, RVM and Rubygems do make somethings more complicated for an average user.

If I want to install an application written in Ruby, I shouldn’t have to install compilers and package managements tools. I want to be able to install a Ruby application and not worry about which ruby/gemset RVM is currently pointed at. These tools are fantastic when you are developing and manageable when your used to them, but if you want to just use a Ruby application without understanding the whole standard development environment they can be a pain in the butt.

What’s the point?

These problems aren’t a huge deal, but it is frustrating to see people claim an argument has no validity because the environment works for them. The Ruby community seems to rush to defend everything that gains popularity without really thinking through the repercussions for people in different situations. It’s this eagerness that drives me to Ruby development, but at time I think this eagerness that will send others rushing away.

Scripting with ruby

11/13/2010

Earlier I wrote about using Unix command line tools to manage text when the job at hand calls for a quick fix instead of a program that you plan to keep around. When the script that I’m writing is a longer one I will often reach for ruby, but ruby can also be quite useful for quick scripts. Specifically, the ruby executable provides several command line flags that are helpful when writing these quick scripts.

-e

-e is the first flag we’ll need for using ruby as our command line swiss army knife. If you call ruby with -e it will evaluate the string following it with the ruby interpreter. Example:

ruby -e 'puts "Hello, world!"'

Got it? Good, now lets move on to more interesting options.

-n, -p, and -i

The -n flag causes ruby to loop over each line of the input. For example if you want to capitalize all of the lines in a file (to stdout) you could do the following:

ruby -n -e 'puts $_.upcase' < original-file.txt > upcased-file.txt

Printing out something is so common that ruby provides another flag that will print out the value of $_ after each step. With -p the following example becomes:

ruby -p -e '$_.upcase!' < original-file.txt > upcased-file.txt

Notice that we are now using the destructive version of upcase (namely upcase!) so that the value of $_ is redefined before it is printed out. It turns out that taking a file, performing some operation on each line, printing the changed line and then putting in a new file is so common that ruby gives us yet another flag to help with this occasion. We can shorten our simple example even further with -i:

ruby -p -i -e '$_.upcase!' file.txt

The -i flag tells ruby to operate on the passed file in-place. This means rather than redirect the file into ruby and the output out of ruby, it will open the file itself and overwrite it with the modified lines. Obviously this isn’t quite the same result as the earlier examples in that the original file is no longer maintained. If you don’t want to lose the original (or you aren’t confident that your script is going to work as expected) you can pass -i a backup extension to make a copy of the original file.

You’ll notice this is similar to the -i flag of sed. I find myself using ruby with -i now whenever I might reach for sed because the -i flag of sed seems to work differently in Linux than in the BSD tools. With ruby I don’t have to worry about the cross-platform stuff as much.

Other Resources

Dave Thomas (of the Pragmatic Programmers) put together a list of handy one liners for ruby. This is old but still quite useful:

http://www.fepus.net/ruby1line.txt

And, as always, the man page has a lot of great information.

Follow

Get every new post delivered to your Inbox.