Ruby 1.9 ã®æ°æ©è½ã®ã²ã¨ã¤ã«ãlambda { ... } ã -> { ... } ã¨æ¸ãããã¨ããã®ãããã¾ãããã®è¡¨è¨ã¯å対æè¦ãæ ¹å¼·ã *1 ã§ããã確å®ã«ãã°ãããç¹ããã£ã¦ãå
¨é¨è¨å·ã ã¨ãããã¨ã§ããããã«ãã£ã¦ Ruby ãè¨å·ã ãã§ãã¥ã¼ãªã³ã°å®å
¨ã«ãªãã¾ã *2 ã
ãã¢ã¨ãã¦ãbrainfuck ã¤ã³ã¿ããªã¿ãè¨å·ã ãã§æ¸ãã¦ã¿ã¾ããã
$___,@_,@__,$_=(@@__="")=~//,?#=~/$/,->(_){_<(__="####"=~/$/)**__&&(@@__<< _;@__[_+@_])},[*$<]*@@__;@__[$___];$____,$_,@___,$__,@__=$_[@_+($_+?!=~/!/ )..-@_],$`,[],[],->(_){(__=$_[_];__=~/[><+\-\.,]/?$__<<$_[_]:__==?[?(@___, $__=$__,[]):__==?]?$__=@___<<$__:__==$\?$\:_)&&@__[_+@_]};@__[$___];@___, $_,@@_,@__=[],[],$___,->(_){$_[@@_]||=$___;({?>=>->{@@_+=@_},?<=>->{@@_-= @_},?+=>->{$_[@@_]+=@_},?-=>->{$_[@@_]-=@_},?.=>->{$><<@@__[$_[@@_]]},?,=> ->{$____=~/^./&&($____=$';$_[@@_]=@@__=~/#{((__,=[*?/..?:]&[$&];__)||(__,= [*?@...?[]&[$&];__)||(__,=[*?`...?{]&[$&];__))&&__!=?{?$&:'\\'+$&}/)},$\=> ->{$\}}[$__[_]]||->{$_[@@_]!=$___&&(@___<<[$__,_-@_];$__=$__[_];@__[$___] *@___,($__,_)=@___);""})[]&&@__[_+@_]};@__[$___]
ãã®ã¨ããã
$ cat hello.bf +++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-. ------------.<++++++++.--------.+++.------.--------.>+. $ ./ruby punctfuck.rb hello.bf; echo Hello, world!
ããã®è¨å·ã´ã«ãã¡ã¼ã«ã¯å¸¸èããããã¾ãããããã¤ã³ãã ãç´¹ä»ãã¾ãã
æ°åãä½ãæ¹æ³
String#=~ ã使ã£ã¦ãã¾ãããã®ã¡ã½ããã¯æ£è¦è¡¨ç¾ããããããå é ä½ç½®ãè¿ãã¾ãããªã®ã§ 1 ãä½ãããã«ã¯
"#"=~/$/
ã¨ããã¾ãã
æ¨æºå ¥åãèªãæ¹æ³
- $< ãæ¨æºå ¥åã® IO ã§ã (æ£ç¢ºã«ã¯ ARGF) ã
- $<.to_a ã§èªã¿è¾¼ãã åè¡ãåè¦ç´ ã«ãããªã¹ããå¾ããã¾ãã
- 1.9 ã§ã¯ [*ary] 㧠ary.to_a ã¨åããããªå¹æãããã¾ãã
- Array#* 㯠Array#join ã¨åãã§ãã
以ä¸ãã¾ã¨ãã¦ã
[*$<]*""
㧠$<.read ã¨åãæååãå¾ããã¾ãã
æ¨æºåºåã«æ¸ãæ¹æ³
1.8 ã§ã¯ $ > < < 65 ã¨ã㧠"A" ãåºåããã¦ãã¾ãããã1.9 ã§ã¯ "65" ãåºåããã¦ãã¾ãã¾ãããªã®ã§ dict = "\x00\x01\x02...\xff" ã¨ããæååãä½ã£ã¦ã$ > < < dict[65] ã¨ããå¿ è¦ãããã¾ããdict ãä½ãã®ã¯ãæ®éã«æ¸ãã°
dict = "" (0..255).each {|i| dict << i }
ã¨ããæãã§ãããå½ç¶ãããªé¢¨ã«ã¯æ¸ããªãã®ã§ãå帰ã§åæåãã¾ãã
dict = "" func = lambda {|i| (dict << i; func.call(i + 1)) if i < 256 } func.call(0)
lambda 㯠-> ã«ãã¦ãProc#call 㯠Proc#[] ã«ãã¦ã
dict = "" func = ->(i) { (dict << i; func[i + 1]) if i < 256 } func[0]
256 㯠4**4 ã«ãã¦ãå¾ç½® if 㯠&& ã«ãã¦ã
dict = "" func = ->(i) { i < 4**4 && (dict << i; func[i + 1]) } func[0]
æ°åã String#=~ ã«ãã¦ã
dict = "" func = ->(i) { i < ("####"=~/$/)**("####"=~/$/) && (dict << i; func[i + ("#"=~/$/)]) } func[""=~//]
å¤æ°åãå ¨é¨è¨å·ã«ãã¦ã
_ = "" __ = ->(___) { ___ < ("####"=~/$/)**("####"=~/$/) && (_ << ___; __[___ + ("#"=~/$/)]) } __[""=~//]
空ç½ãåãé¤ãã°
_="";__=->(___){___<("####"=~/$/)**("####"=~/$/)&& (_<<___;__[___+("#"=~/$/)])};__[""=~//]
ãããããããªããªãã¾ãã
飽ããã®ã§ãã®è¾ºã§ã
追è¨:
åãããã¾ãã (shinh さんありがとう) ã1.8 ã§ã 1.9 ã§ã $ > < < 65 㯠"65" ãåºã¾ããã㨠$><<(""<<65) ã§ååã§ããããã¯ãæ¨æºåºåã®ããã« dict ãä½ãæå³ã¯ãªãã§ãããæ¨æºå
¥åã ASCII ã³ã¼ãã«ç´ãã®ã«ä¸å¿ä½¿ã£ã¦ã¾ããs="A" ãã 65 ã欲ããã¨ãã« dict=~/#{s}/ ã¨ããã¦ããã£ã¡ããªããããæ¹æ³ããããªãã
*1:ruby-core:16611 ãã延ã ã¨ç¶ãè°è«ã¨ãã
*2:cf. Perl ã®「記号だけで eval」 ã