#38 Better Overnight DJ (radio_player2.rb)

This script, radio_player2.rb, is an improvement on radio_player1.rb. Instead of placeholder Procs, it will actually play sound files, as well as log playback with specific times.

The Code

  #a/usr/bin/env ruby
  # radio_player2.rb

❶ LOG_FILE = '/tmp/radio_player2.log'

    '.mp3' => 'mpg321',
    '.ogg' => 'ogg123',
    ''     => 'ls'

❸ # these are variables, local to Kernel.
  # They work just as well as constants.
  play_file_proc = lambda do |filename|    Callbacks
❹   ext = File.extname(filename)
❺   system("#{PLAYERS[ext]} #{filename}") if PLAYERS[ext]
❻   File.open(LOG_FILE, 'a') do |log|
      log.puts([Time.now, filename].join("\t") + "\n")

  dont_play_file_proc = lambda do |filename| puts "I'm not playing #{filename}. ...

Get Ruby by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.