Jason has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Useful Rubyism's Part 1

02.05.2008
| 5080 views |
  • submit to reddit

Ruby is the first language I have programmed in that makes me truly enjoy the development process. It's infinitely flexible, object-oriented, and has a lot of great Rubyism's that make it both unique and powerful. I will demonstrate a few of my favorite Rubyism's over the next few articles.

Class Method Overloading

Don't you just feel dirty every time you have to create a new class type in order to properly encapsulate a custom method that works with native tpes? Or even worse, if you're not into encapsulation, you create a helper function. Well, if you use Ruby, there are no worries. We can simply create our own class methods for the native class types or even overwrite existing class methods.

class String
def shuffle
self.split(//).sort_by{rand}.join
end
end

puts "My Life Is Better Because of Ruby".shuffle
#=> "I iya sMeuB t r ceLoBtfeefuReybs"

One-Liners

Ruby is the king (or queen?) of one-liners. Because nearly everything in Ruby is object-oriented, you can basically write your entire program in one line of code! While that may be a bit extreme, creating one-liners can make your program much more readable and maintainable, especially if you're creating new methods as mentioned above.

#Convert sentence to CSV and capitalize each column
tbdata = "Ruby is awesome"
tbdata.split(/ /).map{|word| word.capitalize}.join(',')
#=> "Ruby,Is,Awesome"

More to Come!

Published at DZone with permission of its author, Jason Palmer.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Kevin Hanrahan replied on Tue, 2008/02/05 - 4:22pm

How do one-liners make code more readable and maintainable?  Can you expand on these comments?   My initial reaction to reading this is that I disagree (and that they make it less readable and arguably less maintainable), but I'm curious to know exactly what you meant. 

 

 

 

Jason Palmer replied on Tue, 2008/02/05 - 5:09pm

class String
def to_caps
self.split(/ /).map {|word| word.to_s.capitalize}.join(' ')
end

def extract_parent_dir
self.split('/')[self.split('/').length-2]
end
end

f = "/usr/etc/scripts/my/my fancy script location/dir"

#Bad
#Extract parent directory and capitalize words
puts f.split('/')[f.split('/').length-2].split(/ /).map {|word| word.to_s.capitalize}.join(' ')

#Good
#Extract parent directory and capitalize words
puts f.extract_parent_dir.to_caps

As you can see, the #Bad line is clearly not readable and not very maintainable. However, if we combine one-liners with properly encapsulated logic we can effectively group our code into business logic blocks. This approach makes our code both maintainable (it's centralized), readable (readable method names, comments, and organizing into business logic units), and flexible.

Jason Palmer
Web Developer & Zone Leader

http://blog.jason-palmer.com/

Kevin Hanrahan replied on Tue, 2008/02/05 - 5:51pm

Ok, with your additional caveat of with "properly encapsulated logic we can effectively group our code into business logic blocks" i agree.  I think this is the point that should be stressed however, because without this I'm not sure your benefits of one liners are ever realized.  Thanks.

Andrew Kirkpatrick replied on Wed, 2008/02/06 - 6:51am

Ruby is the king (or queen?) of one-liners.

 

I'm not convinced that Perl has been dethroned:

ruby -e 'puts "Ruby is OK I guess".split(/ /).map{|word| word.capitalize}.join(",")'

vs

perl -e 'print join(",", map uc, split / /, "Ruby is OK I guess"), "\n"'

That said, everything being an object is nice!

Karmen Blake replied on Wed, 2008/02/06 - 11:27am

I have compiled a list of my own rubyisms too.  This is a fun language.  Go ahead and read them >>

Joyce Elliott replied on Fri, 2008/04/04 - 4:13pm

I definitely like your one liners better Karmen.

 

You should be posting blogs and not just comments here. 

Karmen Blake replied on Sun, 2008/04/20 - 5:43pm in response to: Joyce Elliott

Joyce,  I do post on my blog too.  Check it out: http://blog.dudeblake.com

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.