Guest User

Untitled

a guest
Nov 1st, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.38 KB | None | 0 0
  1. 1.upto 15 do |i|
  2.   (out ||= '') << 'Fizz' if (i % 3).zero?
  3.   (out ||= '') << 'Buzz' if (i % 5).zero?
  4.   puts out || i
  5.   # This will print FizzBuzz as expected
  6. end
  7.  
  8. 1.upto 15 do |i|
  9.   out ||= '' << 'Fizz' if (i % 3).zero?
  10.   out ||= '' << 'Buzz' if (i % 5).zero?
  11.   puts out || i
  12.   # Omitting the parenthesis around the conditional assignment will fail printing FizzBuzz at 15
  13. end
  14.  
Tags: FizzBuzz
Advertisement
Add Comment
Please, Sign In to add comment