ruby笔记之一


1,随意,更象人类的语言
java:
if(a == b) {
System.out. ….//already forgotton? 🙁
}

ruby:
puts “it’s right!” if a == b

2,纯粹的OOP
java:
MyClass my = new MyClass(“param”); //’;’ required
int i = Math.abs(-5);

ruby:
my = MyClass.new(“param”) #no ‘;’ required
i = -5.abs #see, even -5 is an object

3,灵活
java:
public String hello(String sir){
return “hello, ” + sir + “!”;
}

ruby:
def hello(sir)
“hello, #{sir}!” #the last line’s value will be the returned value.
end

4,人性、人性!
任务:打印n个*出来
java:
for(int i=0;i

ruby:
n.times { print “*”}

5,容错
java:
int[] i = {1,2,3};
i[5] = 99; //boom!!! ArrayIndexOutxxxException!!!

ruby:
i = [1,2,3]
i[5] = 99 # i is now [1,2,3,nil,nil,99]

待续……