Ruby Inside
Published in

Ruby Inside

A Weird and Wonderful Trip through Ruby’s Standard Library

Shellwords

$> filename = "Alex's Notes.txt"
$> `cat #{filename}`
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
=> ""
$> `cat #{filename.shellescape}`
=> "Apostrophes in a filename? 🤔"

English: for when you want less $$

$> `exit 42`
=> ""
$> $CHILD_STATUS # or $? for the purists
=> #<Process::Status: pid 25566 exit 42>

Prime

$> 5.prime?
=> true
$> Prime::EratosthenesGenerator.new.take(10)
=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
$> Prime.take(10) # uses Eratosthenes under the hood, by default
=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Abbrev

$> require 'abbrev'
=> true
$> %w(ruby rules).abbrev
=> {
"ruby"=>"ruby",
"rub"=>"ruby",
"rules"=>"rules",
"rule"=>"rules",
"rul"=>"rules"
}
$> names = %w(Alex Amy Ayla Amanda)
$> names.abbrev.keys.select { |n| n.length > 2 }
=> ["Alex", "Ale", "Amy", "Ayla", "Ayl", "Amanda", "Amand", "Aman", "Ama"]

Last, but not least…

$> inbox = Net::POP3.new('pop.gmail.com')
=> #<Net::POP3 pop.gmail.com: open=false>
$> inbox.start('your-email-here@gmail.com', 'supersecret')
=> #<Net::POP3 pop.gmail.com: open=true>
$> inbox.each_mail { |m| puts m.pop.split("\n").grep(/Subject/) }
Subject: Hello IRB!
$> pop.finish
=> "+OK Farewell."

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Taylor

Alex Taylor is a Staff Software Developer at Clio. He has a passion for Ruby, loves tea, and rides his bike (almost) everywhere.