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!
Jan 6 ’11

Prevent String#split from throwing away empty trailing elements

The default behavior of String#split will throw away any trailing values if they are empty.

> "Hello,There,,".split(',') 
=> ["Hello", "There"]

If you want to keep those empty trailing elements, pass a negative number for the second (limit) parameter.

> "Hello,There,,".split(',', -1)
=> ["Hello", "There", "", ""]

This tip was submitted by two-bit-fool.

8 notes 0 comments

  1. voip-insurgency reblogged this from rubyquicktips
  2. markewright reblogged this from rubyquicktips
  3. rubynooby reblogged this from rubyquicktips
  4. atm09td reblogged this from rubyquicktips
  5. iorionda reblogged this from rubyquicktips
  6. two-bit-fool submitted this to rubyquicktips

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