First Ruby code out of a book: 
def num_args( *args )
  length = args.size
  label = length == 1 ? " argument" : " arguments"
  num = length.to_s + label + " ( " + args.inspect + " )"
  num
end
puts num_args
puts num_args(1)
puts num_args( 100, 2.5, "three" )
Java version:
import java.util.Arrays;
public class Test {
 public static void numArgs(Object ...arg){
  int length = arg.length;
  String label=null;
  label = length == 1 ? "argument" : "arguments";
  System.out.println(length+" "+ label+"("+Arrays.toString(arg)+")");
 }
 public static void main(String []args){
  numArgs("1","2","3",4,5,6,3.0);
 }
}
What is the big deal?
Ruby: good or bad dynamic/duck typing vs. Java Object
Ruby: good/bad you need the length.to_s to convert the length int to a string. Java can print out the int without an extra method call. 
Ruby: not sure if this is good or bad, if you put +")" then you get an error, you need a space between the + and ")"
Subscribe to:
Post Comments (Atom)
 
 
No comments:
Post a Comment