Class: FortuneGem

Inherits:
Object
  • Object
show all
Defined in:
lib/fortune_gem.rb,
lib/fortune_gem/version.rb

Constant Summary collapse

VERSION =
"0.0.9"

Class Method Summary collapse

Class Method Details

.fortunesObject



16
17
18
# 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) ⇒ Object



20
21
22
# File 'lib/fortune_gem.rb', line 20

def self.fortunes=(path)
  @fortunes = path
end

.give_fortune(options = {}) ⇒ Object

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



4
5
6
7
8
9
10
11
12
13
14
# 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