Celebrazio Net

Contact Us

Ruby One-Liners

June, 2011

Quick Ruby. No time to explain. Just try them.

ruby -e 'p (1..10).map {|n| n * n * n } '

ruby -rdate -e 'p DateTime.now.cweek'  # gives the calendar week # for the current year

## separate a list 
ruby -e 'p [22, 333, 498, 55, 11,17, 99].partition { |n| n>60|'

##  sum the list 
ruby -e 'p (1..45).inject { |sum,n| sum + n}'

## edit in place 
ruby -pi.bak -e "gsub(/1.5/,'1.6')" v*.txt

"With a PC, I always felt limited by the software available. On Unix, I am limited only by my knowledge."
-- Peter J. Schoenster

1998-2018 Celebrazio.net