Java VS Ruby The Third Impact
Javaで書く基本ループ
public class SampleC {
public static void main(String[] args) {
int i = 0, j = 0;
while(true){
if(i >= 5) break;
System.out.print(” ” + i++);
}
System.out.print(“:”);
do{
System.out.print(” ” + j);
j++;
} while(j < 5);
System.out.println();
for(int m = 0, n = 20; m < n; m += 5,n++){
if(m % 2 == 0) continue;
System.out.println(“m = ” + m + ” n = ” + n);
}}
}
rubyで書く基本ループ
class SampleC
def main
i = j = 0
while true
break if i >= 5
p (” ” + i.to_s)
i += 1
end
p “:”
loop do
p (” ” + j.to_s)
j += 1
break if j < 5
end
p “\n”
m = 0
n = 20
for s in 1..4
m += 5
n += 1
next if m % 2 == 0
p (“m = ” + m.to_s + ” n = ” + n.to_s + “\n”)
end
end
end
x = SampleC.new
x.main
ちなみにRubyは実行環境入れてないのでエラーチェックしてないです。
追記:
チェックしたところ、ダブルクォーテーションが全角なのを除いて
コピペでちゃんと動きました。
コメントを残す