Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

eval でコンパイルエラーを検出

Perl だと、次のようにして $code が Perl プログラムとして妥当かどうか検査できる。

$code = "'hoge' =~ /piyo/";
eval($code);
print "ERROR!" if $@ ne ""; 

さて、Ruby で同様に「ある文字列が Ruby プログラムとして妥当かどうか検査する」にはどうしたらよいか。う〜ん。