[RUBY] Fizzbuzz with ASCII art.

This time, I read a wonderful book called Transcendental Programming, so I tried FizzBuzz with ASCII art. The letters 0x6d61 are written in the square. It's pretty hard to see ...

eval(%w(1.upto(100)do|n|s=n;if(n%15==0);s="FizzBuzz
";e    lsi f(n% 5==   0);s="Buz z";els   if(n% 3==0
); s="F izz "; end ;puts(s);end ;;;; ;;;;;;;;  ;;;;
;; ;  ; ;;;; ;;;;;     ;;;;;    ;;;;     ;;;;; ;;;;
;; ;;;; ;; ;; ;;;; ;;; ;;; ;;;; ;;;; ;;; ;;;;; ;;;;
;;;   ;;; ;;;; ;;;;   ;;;;;;   ;;;;;;   ;;;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;)*"")

Commentary

I will explain the code briefly. First, the main code of fizzbuzz in the `` % w``` part It is decomposed into an array. % w can be converted to ["wei," wei "," wei "] by writing % w (wei wei wei) . Thanks to this mechanism, you can include whitespace in your code. Next, the array is recombined at the *" "` part to make a string. The final code at this stage is below.

"1.upto(100)do|n|s=n;if(n%15==0);s=\"FizzBuzz\";elsif(n%5==0);s=\"Buzz\";elsif(n%3==0);s=\"Fizz\";end;puts(s);end;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"

Finally I'm doing this with eval.

I wrote the following code to create this ruby program. I almost refer to the code of "Transcendental skill programming".

ascii = """
###################################################
###    ### #### ###   ######### ######   ##### ####
## #### ### ## ### ############ #### ########  ####
## #  # #### #####     #####    ####     ##### ####
## #### ## ## #### ### ### #### #### ### ##### ####
###   ### #### ####   ######   ######   ###### ####
###################################################
 """
puts ascii.length
code = <<'END'
1.upto(100) do |n|
s = n;
if(n%15==0);
  s="FizzBuzz";
elsif(n%5==0);
  s="Buzz";
elsif(n%3==0);
    s="Fizz";
end;
puts(s);
end;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
END
p code.length
code = code.split.join
code = 'eval(%w(' + code + ')*"")'
code = ascii.gsub("#") { code.slice!(0,1)}
puts code

As a flow, code is set to one line without spaces, eval is concatenated, and # in ASCII art is replaced with code. Please note that the length of the code you want to execute must be the same as the ASCII art, so in the above code I'm padding with ;.

Finally, I will put the link of paiza. FizzBuzz

Recommended Posts

Fizzbuzz with ASCII art.
ASCII art in Java
Output FizzBuzz with stream
Logic to draw a circle with ASCII art on the console