Useful Rubyism's Part 1
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!
(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
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
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
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