Skip to content

Instantly share code, notes, and snippets.

@zavan
Last active December 3, 2016 23:34
Show Gist options
  • Save zavan/537af9ae1a99498e94efa1bd18b6efd5 to your computer and use it in GitHub Desktop.
Save zavan/537af9ae1a99498e94efa1bd18b6efd5 to your computer and use it in GitHub Desktop.
class Teste
include Enumerable
attr_reader :n
def initialize(n)
@n = valid?(n) ? n.to_i : 100
end
def each
return enum_for :each unless block_given?
(1..n).each { |i| yield i }
end
def print
each { |i| print_text(i) }
end
private
def print_text(i)
if i % 3 == 0 && i % 5 == 0
puts 'Banana Maçã'
elsif i % 3 == 0
puts 'Banana'
elsif i % 5 == 0
puts 'Maçã'
else
puts i
end
end
def valid?(i)
n.to_i > 0
end
end
teste = Teste.new(ARGV[0])
teste.each do { |i| puts "Yielded #{i}!" }
teste.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment