基本数据类型: int, float, boolean ...
java代码中的Object表示什么?
那么 Object.class 又是什么呢?
public void dealWith( Object... objs ){
System.out.println(objs.length);
}
dealWith( 1 )
dealWith( new int[]{ 1,2 })
dealWith( new Integer[]{ 1,2 })
ruby-1.9.2-p290 :001 > iter = 5.times => #<Enumerator: 5:times>
ruby-1.9.2-p290 :002 > iter.next => 0
ruby-1.9.2-p290 :003 > iter.next => 1
"string".class # String "string".class.class # Class "string".class.superclass # Object "string".class.is_a? Module # true "string".class.is_a? Object # true 1.class # Fixnum 1.class.class # Class 1.class.superclass # Integer 1.class.superclass.superclass # Numeric 1.class.superclass.superclass.superclass # Object nil.class # NilClass nil.class.class # Class nil.class.superclass # Object Class.superclass # Module Class.superclass.superclass # Object Object.superclass # BasicObject Object.superclass.superclass # nil
统一的对象模型
关于多态
ruby支持override,但是不支持overload
//overload - java代码示例
public void doSth(Tom obj){...}
public void doSth(Jerry obj){...}
// ruby代码 def doSth(cartoon_figure) do cartoon_figure.doSth end class Tom def doSth ... end end class Jerry def doSth ... end end
需求:list.join(",")
class ExtList implements List{
ExtList(List innerList){...}
public String join(String seperate){...}
}
现有代码全部需要重写,而且会导致ExtList有不同版本
class List def join ... end end
需求:time.yesterday
class Time
def yesterday
self - 86400
end
end需求:{:name => 'john', :age => 31}.only :name
class Hash
def only(*allowed)
reject { |k,v| !allowed.include?(k) }
end
endmodule A
class People
def say
puts "hello"
end
end
class Animal
def say
puts "mie...."
end
end
end
p = A::People.new
p.say # hello
module A
module People
def say
puts "hello"
end
end
module Easter
def eat
puts 'eat'
end
end
end
class Chinese
include A::People
include A::Easter
end
p = Chinese.new
p.say # hello
p.eat # eat
$ irb 1.9.3p194 :001 > class A;end => nil 1.9.3p194 :002 > A.ancestors => [A, Object, Kernel, BasicObject]
对象关系和继承图
# sample.rb
class Parent
def self.make_call
p "called from parent class"
end
end
class Child < Parent
def self.make_call
super
p "called from child Class"
end
end
Parent.make_call
Child.make_call
$ ruby sample.rb "called from parent class" "called from parent class" "called from child Class"
iter = 2.upto(5) while iter.next p 'hello' end
2.upto(5) do p 'hello' end
2.upto(5) do |i| p 'hello'+i end
for(int i =0 ;i<10;i++){
pool.submit({
println i
} as Runnable);
}
结果: 1 2 4 4 5 6 8 8
变量变化,不能按照预期工作
for(int i = 0 ; i<10 ; i++){
final int tempI = i;
pool.submit(new Callable(){
public Object call(){
System.out.println(tempI); return null;
}
});
}
可以工作,但是增加了临时变量,不自然
(0..10).each{
i -> pool.submit( { println i } as Runnable );
}
(0..10).each{
|i| pool.submit { print i };
}
(0..10).each do
|i| pool.submit { print i };
end
总结:block使得变量局部化
Listkeywords = getSomeData(); Map > result = new HashMap<...>(); for (String k: keywords) { char firstChar = k.charAt(0); if (!result.containsKey(firstChar)) { result.put(firstChar, new ArrayList ()); } result.get(firstChar).add(k); } for (List list: result.values()) { Collections.sort(list); }
result = keywords.group_by{ |a| a[0..1] }
从项目里找一段代码
怎么写更好?
class MyFoo
def method_missing(method, *args, &block)
puts %Q[method: #{method}
args: #{args.inspect}
on: #{self.inspect}
]
end
end
ruby-1.9.2-p290 :009 > foo = MyFoo.new ruby-1.9.2-p290 :010 > foo.hello method: hello args: [] on: #<MyFoo:0x0000000144cbf8> ruby-1.9.2-p290 :011 > foo.world method: world args: [] on: #<MyFoo:0x0000000144cbf8>
它能做什么?
ruby版代理模式
class MyProxy
def initialize(real_account)
@subject = real_account
end
def method_missing(name, *args)
puts("Delegating #{name} message to subject.")
@subject.send(name, *args)
end
end
/
#