class FortuneGem

Constants

VERSION

Public Class Methods

fortunes() click to toggle source
# File lib/fortune_gem.rb, line 16
def self.fortunes
  @fortunes ||= File.open("#{File.dirname(__FILE__)}/fortunes").read.split("%").map{|f| f.sub("\n", "").strip }
end
fortunes=(path) click to toggle source
# File lib/fortune_gem.rb, line 20
def self.fortunes=(path)
  @fortunes = path
end
give_fortune(options = {}) click to toggle source

Pass an option of :max_length if you want to limit length of fortunes #

# File lib/fortune_gem.rb, line 4
def self.give_fortune(options = {})

  if options[:max_length]
    short_listed = fortunes.find_all{|f| f.length <= options[:max_length].to_i}
    fortune = short_listed[rand(short_listed.length)]
  else
    fortune = fortunes[rand(fortunes.length)]
  end

  fortune
end