2011.01.02
■[Ruby] ハッシュ記号でクリスマスツリー
Ruby code drawing a Christmas tree in less than 140 characters : programming で、ハッシュ記号を使ってクリスマスツリーを表示するワンライナーが公開されています。
$ ruby -e "((1..20).to_a+[6]*4).each{|i|puts ('#'*i*2).center(80)};puts;puts 'Merry Christmas'.center(80) #Ruby #Christmas "
コードを読んでみると最後の center(80) が、なるほどーっというかんじです。感動。
(1..20).to_a # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] (1..20).to_a+[6]*4 # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 6, 6, 6, 6] ((1..20).to_a+[6]*4).each{|i|puts ('#'*i*2)}; # !> (...) interpreted as grouped expression # >> ## # >> #### # >> ###### # >> ######## # >> ########## # >> ############ # >> ############## # >> ################ # >> ################## # >> #################### # >> ###################### # >> ######################## # >> ########################## # >> ############################ # >> ############################## # >> ################################ # >> ################################## # >> #################################### # >> ###################################### # >> ######################################## # >> ############ # >> ############ # >> ############ # >> ############ ((1..20).to_a+[6]*4).each{|i|puts ('#'*i*2).center(80)}; # >> ## # >> #### # >> ###### # >> ######## # >> ########## # >> ############ # >> ############## # >> ################ # >> ################## # >> #################### # >> ###################### # >> ######################## # >> ########################## # >> ############################ # >> ############################## # >> ################################ # >> ################################## # >> #################################### # >> ###################################### # >> ######################################## # >> ############ # >> ############ # >> ############ # >> ############
