メルマガねた.代替案

(0..23).reject { |hour| hour == 9 or hour == 18 }.each { |hour| ... }全体を囲むのもありか....

class Greeting
  def say
     return "hello!" if Time.now.hour == 9
     return "good bye!" if Time.now.hour == 18
     "hay!"
  end
end

describe Greeting, "at 9:00" do
  before(:each) do
    Time.stub!(:now).and_return(Time.gm(*"2007-10-01-09-00".split("-")))
    @greeting = Greeting.new
  end
  
  it "should say 'hello!'" do
    @greeting.say.should == "hello!"
  end
end

describe Greeting, "at 18:00" do
  before(:each) do
    Time.stub!(:now).and_return(Time.gm(*"2007-10-01-18-00".split("-")))
    @greeting = Greeting.new
  end
  
  it "should say 'good bye!'" do
    @greeting.say.should == "good bye!"
  end
end

(0..23).reject { |hour| hour == 9 or hour == 18 }.each { |hour|  
  describe Greeting, "at #{hour}:00" do
    before(:each) do
      Time.stub!(:now).and_return(Time.gm(*"2007-10-01-#{hour}-00".split("-")))
      @greeting = Greeting.new
    end
    
    it "should say 'hay!'" do
      @greeting.say.should == "hay!"
    end
  end
}