O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#2 Random Signature Generator (random_sig.rb and random_sig-windows.rb)

The next script generates dynamic content for email signatures, adding standard information, like a name and email address, to a random quotation drawn from a known file. The Unix and Windows versions need to be slightly different, so they have been separated into two distinct files. I’ll talk about the Unix version first, but will include the source code for both files. In this example, we’ll also see how Ruby handles complex assignments. That’s a lot of information to cover.

The Code

  #!/usr/bin/env ruby
  # random_sig.rb

❶ filename = ARGV[0] || (ENV['HOME'] + '/scripts/sig_quotes.txt')    Environment Variables; The File Object ❷ quotation_file = File.new(filename, 'r') file_lines ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required