Posts

Showing posts from January, 2006

Rub语言的另一个虚拟机:YARV

YARV主页: http://www.atdot.net/yarv/

Ruby语言经典代码讲座:单态类(Singleton Class)

Image
『》』什么是单态类? 『《』单态类是一种创建性模型类,它用来确保只产生一个实例,并提供一个访问它的全局切入点。 『》』在Ruby语言中,如何写第一个单态类代码? 『《』我们给出一个具体单态类DbConnect代码(线程安全): require 'singleton' class DbConnect include Singleton end 『》』如何调用我们上述的单态类DbConnect? 『《』我们通过irb来实际操作一下: 『》』在Ruby语言中,如何写第二个单态类代码? 『《』我们给出第二个具体单态类DbConnect代码(非线程安全): DbConnect = Object.new class << DbConnect def foo ; end def bar ; end end 『》』如何调用我们第二个单态类DbConnect? 『《』我们还是通过irb来实际操作一下: 『》』在Ruby语言中,如何写第三个单态类代码? 『《』我们给出第三个具体单态类DbConnect代码(非线程安全): module DbConnect def eins;end def zwei;end module_function :eins,:zwei end 等价代码为: DbConnect = Module.new class << DbConnect def eins;end def zwei;end end module DbConnect private def eins;end def zwei;end end

Ruby语言经典代码讲座:方法的多个返回值

『》』 如何处理方法的多个返回值? 『《』我们的代码如下: class Auto @width = 0 @height = 0 def size @width,@height = 20, 30 end end auto = Auto.new p = auto.size puts p[1] puts p.class puts p[1].class #=>30 #=>Array #=>Fixnum 说明: (一)方法可以返回多个值,它是返回一个Array类的实例;

Ruby语言经典代码讲座:理解类

『》』理解类 『《』我们的实例代码如下: class Auto def Auto.max_length puts "我是Auto.max_length!" end def length puts "我是Auto.new.length!" end end Auto.max_length Auto.new.length #=>我是Auto.max_length! #=>我是Auto.new.length! 说明: (一)我们可以使用类名Auto调用方法; (二)我们也可以使用new调用方法; (三)我们为什么要 直接 使用类名调用方法;