10 Ruby One Liners to Impress Your Friends

Someone came up with a list of 10 one-liner examples that are meant to showcase Scala’s expressiveness. A CoffeeScript version quickly emerged, so I thought I’d publish a Ruby one. I find Ruby’s syntax to be a bit cleaner than Scala’s, but the substance (at least as far as these examples are concerned) is relatively similar.

Multiply each item in a list by 2

(1..10).map { |n| n * 2 }

Sum a list of numbers

(1..1000).inject { |sum, n| sum + n }

Or using the (built in) Symbol#to_proc syntax that’s been available since Ruby 1.8.7:

(1..1000).inject(&:+)

Or even just passing a symbol directly:

(1..1000).inject(:+)

Verify if tokens exist in a string

words = ["scala", "akka", "play framework", "sbt", "typesafe"]
tweet = "This is an example tweet talking about scala and sbt."

words.any? { |word| tweet.include?(word) }

Reading a file

file_text = File.read("data.txt")
file_lines = File.readlines("data.txt")

The latter includes “\n” at the end of each element of the array, which can be trimmed by appending .map { |str| str.chop } or by using the alternative version:

File.read("data.txt").split(/\n/)

Happy Birthday

4.times { |n| puts "Happy Birthday #{n==2 ? "dear Tony" : "to You"}" }

Filter a list of numbers

[49, 58, 76, 82, 88, 90].partition { |n| n > 60 }

Fetch and parse an XML web service

require 'open-uri'
require 'hpricot'

results = Hpricot(open("https://search.twitter.com/search.atom?&q=scala"))

This example requires open-uri and hpricot or equivalent libraries (you could use builtin ones if you wish). It’s not too much code, but Scala clearly wins here.

Find minimum (or maximum) in a list

[14, 35, -7, 46, 98].min
[14, 35, -7, 46, 98].max

Parallel Processing

require 'parallel'

Parallel.map(lots_of_data) do |chunk|
  heavy_computation(chunk)
end

Unlike Scala, multicore support is not built-in. It requires parallel or a similar gem.

Sieve of Eratosthenes

The Scala one liner is very clever, but entirely unreadable. A simpler implementation that is no longer a one-liner in Ruby would be:

index = 0
while primes[index]**2 <= primes.last
      prime = primes[index]
      primes = primes.select { |x| x == prime || x % prime != 0 }
      index += 1
end
p primes

This last example is straight from StackOverflow. Not the prettiest code ever, but you get the idea.

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.

22 Comments

  1. Florian Gilcher June 2, 2011
    • Antonio Cangiano June 2, 2011
    • glenn mcdonald June 2, 2011
  2. Florian Gilcher June 2, 2011
    • Antonio Cangiano June 2, 2011
    • swapnil patil May 30, 2012
  3. josh susser June 2, 2011
    • Antonio Cangiano June 2, 2011
  4. brainopia June 2, 2011
    • Antonio Cangiano June 2, 2011
  5. Roger Braun June 2, 2011
    • Roger Braun June 2, 2011
  6. Joel Meador June 2, 2011
  7. David June 2, 2011
  8. nobu June 2, 2011
  9. Cyrus June 2, 2011
  10. al June 3, 2011
    • Antonio Cangiano June 3, 2011
  11. steve June 3, 2011
  12. Sijo June 9, 2011
  13. oldmoe June 10, 2011
  14. doel May 29, 2012
  15. Pingback: Swift ? 10 ??????????? | ???? April 19, 2016

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.