Skip to content

Instantly share code, notes, and snippets.

@cesare
Created May 23, 2012 15:02
Show Gist options
  • Save cesare/2775740 to your computer and use it in GitHub Desktop.
Save cesare/2775740 to your computer and use it in GitHub Desktop.
#
# Nabeatsu
# returns "hoge" if given value is a multiple of 3, or contains '3' in it; otherwise just the value itself (in String).
#
# Restriction of coding:
# DO NOT use conditional branching syntax, such as "if", "unless", "case ... when".
#
module Nabeatsu
class << self
def say(n)
methods = [:hoge, :say_by_containing3, :say_by_containing3]
method = methods[n % 3]
self.send(method, n)
end
def hoge(n)
"hoge"
end
def just(n)
n.to_s
end
def bool_to_int(true_or_false)
{ false => 0,
true => 1,
}[true_or_false]
end
def say_by_containing3(n)
methods = [:just, :hoge]
method = methods[bool_to_int(split_digit(n).member?(3))]
self.send(method, n)
end
def split_digit(n)
methods = [:empty_array, :split_next]
method = methods[bool_to_int(n > 0)]
self.send(method, n)
end
def split_next(n)
split_digit(n / 10) << n % 10
end
def empty_array(n)
[]
end
end
end
require File.join(File.dirname(__FILE__), "nabeatsu")
describe Nabeatsu do
subject { Nabeatsu.say(n) }
describe ".say" do
context "given 1" do
let(:n) { 1 }
it { should == "1" }
end
context "given 3" do
let(:n) { 3 }
it { should == "hoge" }
end
context "given 4" do
let(:n) { 4 }
it { should == "4" }
end
context "given 6" do
let(:n) { 6 }
it { should == "hoge" }
end
context "given 12" do
let(:n) { 12 }
it { should == "hoge" }
end
context "given 13" do
let(:n) { 13 }
it { should == "hoge" }
end
context "given 20" do
let(:n) { 20 }
it { should == "20" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment