7 ms·
#!/usr/bin/env ruby puts((1..100).map do |n| "#{:Fizz if (n % 3).zero?}"\ "#{:Buzz if (n % 5).zero?}" .then .find { |s| !s.empty? } || n
by smitherfield 8y ago
#!/usr/bin/env ruby
puts((1..100).map do |n|
"#{:Fizz if (n % 3).zero?}"\
"#{:Buzz if (n % 5).zero?}"
.then
.find { |s| !s.empty? } || n
end)
- jpatokal 8y agoI'm curious: do you genuinely find that beautiful?To me all the enumerable/map/frozen string literal stuff seems pointlessly overcomplicated compared to the simplicity of just writing down what you're supposed to do: (1..100).each do |n| a = String.new a << "Fizz" if n%3 == 0 a << "Buzz" if n%5 == 0 a << n.to_s if a.empty? puts a end (Disclaimer: not my code, on mobile so I stole that off GitHub)
- smitherfield 8y agoNot really; I was basing it off of https://news.ycombinator.com/item?id=18873581#18875961 https://news.ycombinator.com/item?id=18873581#18875961 as a semi-joke. I changed it to a relatively KISS and IMO genuinely beautiful version in the parent, for others' benefits here's the version you were referring to in the above comment: #!/usr/bin/env ruby # frozen_string_literal: true module FizzBuzz Integer.include self def self.array(enumerable = 1..100) enumerable.map(&:to_fizzbuzz) end def to_fizzbuzz "#{:Fizz if (self % 3).zero?}"\ "#{:Buzz if (self % 5).zero?}" .then .find { |s| !s.empty? } || to_s end end puts FizzBuzz.array
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]