Java VS Ruby Returns

JAVAの教科書にあったソース

class Dog {
 int weight = 50;
 static {
  System.out.println(“static inr”);
 }
 {
  System.out.print(“inr “);
 }
 Dog() {
  System.out.println(“ctr”);
 }
 void meth() {
  System.out.println(weight);
 }

}

class SampleA {
 public static void main(String[] args){
  Dog x = new Dog();
  Dog y = new Dog();
  x.meth();
 }
}

Rubyに翻訳

class Dog
 def initialize
  @weight = 50
  p “inr ctr”
 end
 def meth
  p @weight
 end
end

p “static inr”
x = Dog.new
y = Dog.new
x.meth

 もういっちょJAVA

import java.util.*;

class SampleB {
 public static void main(String[] args){
  int[] ds = {1, 2, 3, 4, 5};
  for(int d : ds)
   System.out.println(d);
  System.out.println();
  int[][] nns = {{1, 2, 3}, {10, 20, 30}, {100, 200, 300}};
  for(int[] ns : nns){
   for(int n : ns){
    System.out.println(n);
   }
   System.out.println();
  }
  List<String> ss = new ArrayList<String>();
  ss.add(“abc”);
  ss.add(“def”);
  ss.add(“ghi”);
  for(String s : ss)
   System.out.println(s);
  
 }
}

もういっちょRuby

class SampleB
 def main
  ds = [1, 2, 3, 4, 5]
  for d in ds
   p d
  end
  p “”
  nns = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
  for ns in nns
   for n in ns
    p n
   end
   p “”
  end
  ss = []
  ss.push(“abc”)
  ss.push(“def”)
  ss.push(“ghi”)
  for s in ss
   p s
  end
 end
end

x = SampleB.new
x.main

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*