å¤æ¥ããï¼ã½ã¼ã¹ã³ã¼ãã®ã¤ã³ãã³ãã¯äººåã§è¡ããã¦ããï¼ã¨ãã£ã¿ãã¨ãã¤ããã°ã©ã è¨èªãã¨ã«ããã°ã£ã¦ã¤ã³ãã³ãã®ããã°ã©ã ãæ¸ããã¦ããï¼Emacsã«Rubyç¨ã®ã¤ã³ãã³ãã®ããã°ã©ã ã¨ãPerlç¨ã®ã¤ã³ãã³ãã®ããã°ã©ã ããã£ã¦ï¼Vimã«ãä¼¼ããããªã®ãRubyç¨ã¨ãPerlç¨ã¨ãã¡ã¾ã¡ã¾ç¨æããã¦ãï¼Emacsã®ruby-mode.elã ã¨ï¼ã«ã¼ã½ã«ããã£ãã®ä¸ã«ããããããããã¨ãã§ï¼è·äººã£ã½ãï¼
人éãããããã®ãæ¸ããªãã¦ãï¼å¨ãã®ã½ã¼ã¹ã³ã¼ãã解æãããï¼æ®éã¯ããããå ´é¢ã§ã¯ã¤ã³ãã³ãããï¼ã¨ããã®ãæ©æ¢°çã«ã§ããã ããã¨æã£ãï¼
以ä¸ã®Perlã®ã³ã¼ãã¯ã¹ã¤ã«ã¤ã³ãã³ãããããªãã¨æãï¼
print 1; print 2;
以ä¸ã®Perlã®ã³ã¼ãè¦ããï¼2è¡ç®ã§ã¤ã³ãã³ããã¦ï¼3è¡ç®ã§æ»ããããªãã¨æãï¼
if ($i % 15 == 0) { print "FizzBuzz\n"; }
ããããããªãã¨æãï¼
if ($i % 15 == 0) { print "FizzBuzz\n"; }
ãããæ©æ¢°ãå¤å®ã§ããããã«ãããï¼
åè¡ã®è¡æ«ã®1æåã¨ï¼æ¬¡ã®è¡ã®å
é ã®ã¹ãã¼ã¹ä»¥å¤ã®1æåã®çµã«å¯¾ãã¦ï¼ã¤ã³ãã³ããå¤åãããã©ããã®ãã¼ã¿ãããã°ï¼ãã®ãã¼ã¿ã使ã£ã¦èªåçã«ã¤ã³ãã³ãã§ããã¨æã£ãï¼
ä¸ã®ä¾ã®ã¤ã³ãã³ãã¯ï¼ä»¥ä¸ã®ãã¼ãã«ã®ã«ã¼ã«ã§è¡ããã¦ããã¨è¨ããï¼;ã®æ¬¡ã«pãæ¥ãã¨ãã¯å¤åãªãï¼{ã®æ¬¡ã«pãæ¥ãã¨ãã¯4ã¤å³ã«è¡ãï¼;ã®æ¬¡ã«}ãæ¥ãã¨ãã¯4ã¤å·¦ã«è¡ãï¼
è¡æ«ã®æå | è¡é ã®æå | ã¤ã³ãã³ãã®å¤å |
---|---|---|
; | p | 0 |
{ | p | 4 |
; | } | -4 |
åå¦çã¨ãã¦ï¼ã³ã¼ããããããéãã¦ï¼ãããã®ã¤ã³ãã³ãã®é°å²æ°ããã¡ã¤ã«ã«æ¸ãåºãã¦ããï¼ãã®ãã¡ã¤ã«ã使ã£ã¦ï¼ä»ã®ã½ã¼ã¹ã³ã¼ããã¤ã³ãã³ããããã¤ãä½ã£ã¦ã¿ãï¼
Plackã®ã½ã¼ã¹ã³ã¼ããéãã¦ã¤ã³ãã³ããã¦ãªãFizzBuzzãã¤ã³ãã³ããã¦ã¿ãï¼
Plackã®ã¤ã³ãã³ãæ å ±ã調ã¹ã¦ãã¡ã¤ã«ã«æ¸ãåºãï¼
% find ~/Plack/lib/ | grep '\.pm$' | xargs ruby learn.rb > examples/learned_plack.txt
ãããªé°å²æ°ã®ãã¡ã¤ã«ãã§ããï¼
),),-4 ,,},-4 ,,),-4 ,,],-4 ,,f,4 t,w,4 t,},-4
ãã¨ãã°ï¼),),-4ã¯ï¼åã®è¡ã®è¡æ«ã)ã§æ¬¡ã®è¡ã®è¡é ã)ãªãï¼æ¬¡ã®è¡ã¯4æåå·¦ã«ã¤ã³ãã³ããæ»ããã¨ãå¤ãï¼ã¨ããæå³ï¼
å
¥åãããã½ã¼ã¹ã³ã¼ããå
¨è¡èª¿ã¹ã¦ï¼2æåã®çµããããã«ã¤ãã¦ï¼æé »å¤ã®ã¤ã³ãã³ãã®å¤åãä¿åãã¦ããï¼
å®éã«ã¯),)ã§0ã®è¡ããã£ãã ãããã©ï¼-4ãä¸çªå¤ãã£ãï¼ã¨ããæãï¼
ã¤ã³ãã³ããã¦ãªãã½ã¼ã¹ã³ã¼ããããªã®ï¼
use strict; use warnings; for my $i (1..30) { if ($i % 15 == 0) { print "FizzBuzz\n"; } elsif ($i % 3 == 0 ) { print "Fizz\n"; } elsif ($i % 5 == 0 ) { print "Buzz\n"; } else { print "$i\n"; } }
Plackã®ã¤ã³ãã³ãæ å ±ã使ã£ã¦ã¤ã³ãã³ãããï¼
% ruby format.rb examples/learned_plack.txt examples/fizz_buzz.pl
ãã¾ãã¤ã³ãã³ããããï¼ãããï¼
use strict; use warnings; for my $i (1..30) { if ($i % 15 == 0) { print "FizzBuzz\n"; } elsif ($i % 3 == 0 ) { print "Fizz\n"; } elsif ($i % 5 == 0 ) { print "Buzz\n"; } else { print "$i\n"; } }
FizzBuzzã¯ç´ æ´ã ã£ããããã¾ããã£ããã©ï¼æ®éã®ã¯ã©ã¹ã¨ããããã¨ï¼å¤±æãã¦ï¼ã ãã ãå³ã«ããã¦ãã£ãããã¦æããï¼æ¬å½ã¯unless defined $env && ref($env) eq 'HASH';
ã®æ¬¡ã®è¡ã§å·¦ã«æ»ã£ã¦ã»ããï¼
package Plack::Request; use strict; use warnings; use 5.008_001; our $VERSION = '0.9985'; $VERSION = eval $VERSION; use HTTP::Headers; use Carp (); use Hash::MultiValue; use HTTP::Body; use Plack::Request::Upload; use Plack::TempBuffer; use URI; use URI::Escape (); sub _deprecated { my $alt = shift; my $method = (caller(1))[3]; Carp::carp("$method is deprecated. Use '$alt' instead."); } sub new { my($class, $env) = @_; Carp::croak(q{$env is required}) unless defined $env && ref($env) eq 'HASH'; bless { env => $env }, $class; } sub env { $_[0]->{env} } sub address { $_[0]->env->{REMOTE_ADDR} } sub remote_host { $_[0]->env->{REMOTE_HOST} }
Perlã§ã¯ãã¾ããã£ããã©ï¼Rubyã§ã¯ãã¾ããããªãã£ãï¼è¨å·ããªãã¨ãã¾ããããªãããï¼Rubyã¯ifã«è¨å·ãªãã®ã§ï¼è¡é ï¼è¡æ«ã«ã¤ã³ãã³ãã®ç¹å¾´ããã¾ããªãã¨æãï¼
1.upto(30) { |i| if i % 15 == 0 puts 'FizBuzz' elsif i % 3 == 0 puts 'Fizz' elsif i % 5 == 0 puts 'Buzz' else puts i end }
ç´ æ´ãªæãã«ä½ã£ã¦ã¿ããPerlã®FizzBuzzã¯ãã¾ãã¤ã³ãã³ãã§ãã¦ãããããã£ãï¼
ãã©Rubyã§ã¯åããªãã¨ãï¼Perlã§ãã©ãã©ãå³ã«ããã¦ããã¨ãã§ï¼ã¾ã å®ç¨çã§ãªãã®ã§ï¼ããã¡ãã£ã¨ãªãã¨ããããï¼
åè¡ããã®å¤åãªã®ã§ï¼ééãã¨ä¸ã¯å
¨é¨ãããã®ã§ï¼ä¸ã«è¡ãã»ã©ãããèç©ãããï¼
ã¤ã³ãã³ãã®ã«ã¼ã«ãçµ±è¨çã«æ±ºãããããï¼ã¨ãã£ã¿ããã¯ãã®ãã¼ã¿ã使ãã°ããã®ã§ï¼ã¨ãã£ã¿ããã¤ã³ãã³ãã®ã¢ã«ã´ãªãºã ãåãé¢ãã¦ä¾¿å©ã ã¨æãï¼
ãã¾ããã£ããï¼ã³ã¼ãã¬ãã¥ã¼ã®éã«ï¼ã³ã¼ãã®ã¹ã¿ã¤ã«ãããã ãããããï¼ã¨ããããã§æãã¦ãããããã«ãªãäºå®ï¼
追è¨
åããããªæ¹æ³ã§ã¤ã³ãã³ããã§ãã«ã¼ä½ã£ãï¼