Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jun 2 ’11

Spread command chains across multiple lines

Ruby’s syntax allows you to spread command chains across multiple lines:

puts "You can do a lot with this in one line".reverse.sub('eno', 'elpitlum').sub(' htiw tol a', '').reverse.<< 's, too!'

…can also be written like this:

puts "You can do a lot with this in one line".
reverse.
sub('eno', 'elpitlum').
sub(' htiw tol a', '').
reverse.
<< 's, too!'

In Ruby < 1.9 you have to place the dot at the end of the line, like shown in the example above. From Ruby 1.9 on, you can also place the dot at the beginning of the next line, right before the next command call.

1 note 0 comments

  1. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus