Skip to content

Instantly share code, notes, and snippets.

@gtyanchev

gtyanchev/rat.rb Secret

Created November 8, 2012 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtyanchev/e33056417ac936729d29 to your computer and use it in GitHub Desktop.
Save gtyanchev/e33056417ac936729d29 to your computer and use it in GitHub Desktop.
A ruby rational numbers class with automated minitest
class Rat
attr_reader :num, :den
def initialize(num, den)
gcd = num.gcd den
@num = num / gcd
@den = den / gcd
end
def ==(other)
num == other.num and den == other.den
end
def +(other)
Rat.new num * other.den + other.num * den, den * other.den
end
def -(other)
self + -other
end
def -@
Rat.new(-num, den)
end
end
require 'minitest/autorun'
require './rat'
class RatTest < MiniTest::Unit::TestCase
def test_initilizing
assert_equal 1, Rat.new(1, 2).num
assert_equal 2, Rat.new(1, 2).den
end
def test_equality
assert_equal Rat.new(3, 2), Rat.new(3, 2)
assert_equal Rat.new(6, 4), Rat.new(3, 2)
refute_equal Rat.new(3, 2), Rat.new(1, 2)
refute_equal Rat.new(1, 3), Rat.new(1, 2)
end
def test_addition
assert_equal Rat.new(1, 2) + Rat.new(3, 4), Rat.new(5, 4)
assert_equal Rat.new(1, 2) + Rat.new(1, 2), Rat.new(1, 1)
end
def test_subtraction
assert_equal Rat.new(3, 4) - Rat.new(1, 2), Rat.new(1, 4)
end
def test_if_negative_sign_is_in_the_numerator
assert_equal -Rat.new(3, 4), Rat.new(-3, 4)
end
end
watch('rat\.rb') do # Посочваме че искаме да "следим" rat.rb, който е в текущата директория
system 'clear' # Изчиства текущия output в конзолата.
success = system 'ruby tests.rb' # Връща истина, ако теста tests.rb се е изпълнил без грешка
if success
#Ако тестът е успешен използвам командата notify-send, която извежда съобщение на екрана
#Съобщението остава видимо 10 секунди, съдържанието му е output-а от изпълнението на minitest файла (tests.rb)
system 'notify-send -t 10000 -i /home/captain/Downloads/icos/tick.png "`ruby tests.rb | tail -1`"'
else
system 'notify-send -t 10000 -i /home/captain/Downloads/icos/error.png "`ruby tests.rb | grep -A 1 Failure`"'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment