(ãã㯠Ruby Advent Calendar jp: 2009 ã® 16 æ¥ç®ã®è¨äºã§ãã)
Ruby ã¯èªã¿ãããããã°ã©ã ãç°¡åã«æ¸ããè¨èªã§ãããèªã¿ã«ããããã°ã©ã ãç°¡åã«æ¸ãã¾ãã
ä»åã¯ãRuby ã§ã§ãããããããªé£èªåã®ãã¯ããã¯ã 4 ã¤ç´¹ä»ãã¾ãã
1. Integer#to_s ã使ãæ¹æ³
Integer#to_s *1 ã¯ãä½é²æ°ã¨ãã¦æåååããããå¼æ°ã§æå®ã§ãã¾ããæ®é㯠2 ã10 ã16 ããããã使ããªãã¨æãã¾ãããå®ã¯ 36 ã¾ã§æå®ã§ãã¾ãã
0.to_s(36) #=> "0" 1.to_s(36) #=> "1" ... 8.to_s(36) #=> "8" 9.to_s(36) #=> "9" 10.to_s(36) #=> "a" 11.to_s(36) #=> "b" ... 34.to_s(36) #=> "y" 35.to_s(36) #=> "z" 36.to_s(36) #=> "10"
ãã®ããã«ã0-9 ã a-z ã ãã®æååãçæãããã¨ãã§ãã¾ããä¾ãã°ã
p "helloworld".to_i(36) #=> 1767707668033969
ãªã®ã§ã
puts 1767707668033969.to_s(36) #=> helloworld
ãªã©ã¨æ¸ãã¾ããã±ã£ã¨è¦ã§ã¯ä½ãåºããããããªãã§ãããã
å®ä¾ã
puts "Ruby Advent Calendar jp: 2009"
â
puts "R" + "uby" + " A" + "dvent" + " C" + "alendar" + "jp" + ": " + "2009"
â
a = ["uby", "dvent", "alendar", "jp", "2009"] puts ["R", " A", " C", " ", ": "].zip(a).join
â
a = [37169,20855234,19507626802,690,85759].map{|s|s.to_s(35)} puts %w(R \ A \ C \ :\ ).zip(a).join
â
puts %w(R \ A \ C \ :\ ).zip([109* 11*31,2*103*3491* 29,178*109593409, 690,85759 ].map{| c|c.to_s(35)})*""
to_s ã使ãä»ã®ã¢ããã¼ãã§ã¯ããããªã®ãæ¸ãã¾ããèªã¿è§£ãã¦ã¿ã¦ãã ããã
puts"ujf9z30ia ehch14v8sfrqw1 h0fyq".gsub(/[ ]/,"").to_i(36 ).to_s(35).tr\ "13456","R AC:"
2. %w() ãªãã©ã«ã使ãæ¹æ³
ãã¾ãç¥ããã¦ãã¾ãããã%w() ã¨ãããªãã©ã«ãããã¾ããæååã®é åãçæãã¾ãã
%w(foo bar baz) #=> ["foo","bar","baz"]
ããã使ãã¨ãããã°ã©ã ãã¢ã¹ãã¼ã¢ã¼ãåãã¦é£èªåã§ãã¾ããã¾ãã¯
puts "Ruby Advent Calendar jp: 2009"
ãå¤ãã¦ãã¹ãã¼ã¹ã使ããªãã§ããã°ã©ã ãæ¸ãã¾ã *2 ã" " ãã»ãããã° 32.chr (ã¹ãã¼ã¹ã® ASCII ã³ã¼ã) ãªã©ã§ä½ãã¾ããæ¹è¡ã¯ã»ãã³ãã³ãªã©ã§ä»£ç¨ãã¦ãã ããã
puts"Ruby_Advent_Calendar_jp:_2009".tr("_",32.chr)
ããã eval ã§ããã¿ã¾ãã
eval('puts"Ruby_Advent_Calendar_jp:_2009".tr("_",32.chr)')
ã³ã¼ãé¨åãã%w() ãªãã©ã« + join ã«ç½®ãæãã¾ãã
eval(%w(puts"Ruby_Advent_Calendar_jp:_2009".tr("_",32.chr)).join)
ãã¨ã¯ã%w ã®ä¸ã«ç©ºç½å ¥ãæ¾é¡ãæ¹è¡ãæ¾é¡ã§ãããã¼ã¯ã¼ãã®éä¸ã§ãã¡ã½ããåã®éä¸ã§ã OK ã§ããèªç±ã«æ´å½¢ãã¦ãã ããã
eval(%w( put s"R ub y_ Adve nt_C al en da r_ jp :_ 20 09 ".tr ("_" ,3 2. chr )). join)#xx
eval( %w(pu ts"Ru by_Ad vent_ Calen dar_j p:_20 09".g sub(" _",32 .chr) )*"")
ã¨ãã
3. æå¤ãªååã®ã¡ã½ããã使ãæ¹æ³
ã¡ã½ããã§ãããã¨ãå¿ããã¡ãªã¡ã½ããã使ãã¨ãèªã¿æãæ··ä¹±ãããããã¨æãã¾ãã
alias ` puts `Ruby Advent Calendar jp: 2009`
ã¨ãã
alias / send "Ruby Advent Calendar jp: 2009\n" / "display"
ã¨ããInteger#to_s ã¨çµã¿åãããã¨ãè¦ãã ãã§ã¯ä½ãèµ·ããã®ãå ¨ãããããªããªãã§ãããã
alias / send "Ruby Advent Calendar jp: 2009 " / 24885962359.to_s(35)
1.9 å°ç¨ã§ããããããªã®ãæ¸ãã¾ãã
# coding: UTF-8 (ããã²ãã="Ruby Advent Calendar jp: 2009"; alias/send;alias|display;alias ã¾ã¤ã㨠puts ã¾ã¤ã㨠ããã²ãã/:|)
(âãã¡ãããã® signature ã¯å½ç©ã§ããmatz ã¨ã¯é¢ä¿ããã¾ããã念ã®ããã)
4. ãã¼ã«ã使ãæ¹æ³
_ ã使ãã°ãä»»æã® Ruby ããã°ã©ã ã _ ã ãã«ã§ãã¾ããã¨ã¦ãæ軽ã«æå³ä¸æã«ã§ãã¾ãã
ref: http://d.hatena.ne.jp/ku-ma-me/20091115/p1
$ gem19 install _ $ ruby19 -r_ -e 'puts __script__("puts \"Ruby Advent Calendar jp: 2009\"")' > rac2009.rb
ãããªãã¾ãã
require "_" ____ _ _____ ____ __ ____ ____ __ ___ ____ __ __ _ ______ ___ _ ______ _____ ___ __ _____ ____ __ ____ ___ _____ ___ ____ ___ __ _ ______ ___ __ _____ ______ ___ _____ _____ ____ __ _____ ___ _____ ______ ____ _ ___ ____ __ ___ _ ______ ___ __ ______ __ ___ _____ __ ____ _ _ ___ _____ ______ ____ _ ___ ___ _____ _____ ___ _____ __ ____ __ _ _ ______ ___ ___ ______ _____ ____ _ _____ __ ____ _____ _ ______ ___ __ ___ ___ __ ___ _ __ ___ _ __ ____ ____ _ ______ _____
å®è¡ããã¨ã¡ããã¨åãã¾ãã
$ ruby19 rac2009.rb Ruby Advent Calendar jp: 2009
ã»ãã«ã任意の Ruby プログラムを記号だけにするプログラム (id:kurimura さん)ã任意の Ruby プログラムをアルファベットと数字だけにするプログラム (id:shinichiro_h さん)ã使ããã¨æãã¾ãã
ã¾ã¨ã
ãããã㪠Ruby ã³ã¼ãã®é£èªåæ¹æ³ãç´¹ä»ãã¾ããã
ã©ããåç´ãªæ¹æ³ãªã®ã§ãç¥ã£ã¦ããã°ããã«èªã¿è§£ãã¦ãã¾ãã¾ã (解èªãã¼ã«ãä½ãã¦ãã¾ã) ããRuby ã«è©³ãããªã人ã«ã¯ãããªãã«å¹æãããã¨æãã¾ãã
Ruby Advent Calendar jp: 2009 ã§ã¯ã¿ããªæ¬å½ã«å½¹ã«ç«ã¡ãã㪠tips ã°ã£ããç´¹ä»ãã¦ããã®ã§ããããã tips ãããããªã¨ã
ãã¾ã: Array#pack ã使ãæ¹æ³
å ã®æååã 64 種é¡ä»¥ä¸ã®æåã§æ§æããã¦ããå ´åã¯ããããªæ¹æ³ãããã¾ãã
puts ["Fj2T'oz{UPI^91+V:{Wm4}UU"].pack("m").tr("VEmCTUOfo7", " auAdCn9b:").strip
ä»åã®ããã«æ¸ãã¦ã¿ãã®ã§ãããå ¨ç¶ç°¡åã§ã¯ãªãã£ãã®ã§ç´¹ä»ã ã *3 ãä½ãæ¹ãæ´çãã¦å¥ã®æ©ä¼ã«ç´¹ä»ãããããããªãããã