First glance at Ruby

大年三十的时候,上班已经没什么事儿了,就仔细读了读以前保存的很多不错的文章,包括这一篇"Ruby, PHP and a Conference",作者是大名鼎鼎的Bruce Eckle,"Thinking in Java"这个经典教材的作者。

一直就很想看看Ruby这个很火的语言,这两天终于有些时间,就找了一些文章看了看。

强烈推荐一篇"10 Things Every Java Programmer Should Know About Ruby",挺不错。

总结一下Ruby吸引我的地方:

1) Case Statement

In Ruby, the case statement can match with any object. It uses the “===” method in Object to perform the match, which can be overridden for your own classes. Since everything is really an object in Ruby, this includes any type of literal, regular expressions, and object types.

2) Easy Reflection

Eclipse Plugin Framework中,实例化一个插件类的代码是:

  1. try {
  2.    
  3.         //    用每个插件自己的PluginClassLoader来得到这个插件的主类
  4.        
  5.         pluginClass = descr.getPluginClassLoader().loadClass(
  6.                 className);
  7.     } catch (ClassNotFoundException cnfe) {
  8.         badPlugins_.add(descr.getId());
  9.         throw new PluginException("can't find plug-in class " + className);
  10.     }
  11.     try {
  12.         Class pluginManagerClass = getClass();
  13.         Class pluginDescriptorClass = IPluginDescriptor.class;
  14.  
  15.         Constructor constructor = pluginClass
  16.                 .getConstructor(new Class[] { pluginManagerClass,
  17.                         pluginDescriptorClass });
  18.  
  19.         //    调用插件默认的构造函数
  20.         //    Plugin(PluginManager, IPluginDescriptor);
  21.        
  22.         result = (Plugin) constructor.newInstance(new Object[] {
  23.                 this, descr });
  24.     } catch (InvocationTargetException ite) {
  25.         ... ...
  26.     } catch (Exception e) {
  27.         ... ...
  28.     }

在Ruby中,Reflection变的异常简单:

  1. def create(klass, value)
  2.     klass.new(value)
  3.   end
  4.  
  5.   g = create(Greeting, "Hello")

3) Everything is Message

任何函数调用都是消息,object.test(arg1)就是像object发送test消息,参数为arg1。这样带来的好处是,这多一层的抽象性,可以让你有更多的控制权。

比如:

Remote Proxies
Automatically forward any message to a remote object.
Auto Loaders
Stand in for an object until it gets its first message. Then load it and act like a regular proxy. Great for autoloading database backed objects.
Decorators
Intercept the messages you want and pass the rest through.
Mock Objects
Just write the methods that need to be mocked. Proxy or ignore the others as needed.
Builders
Generate XML/HTML/Whatever based on the methods called on the builder

4) Block

大米曾经讲过一次Block,我还是觉得Block就是Command模式的应用,当然,yield已经是嵌入到Ruby语言中的关键字了。

5) Open Classes

这算是一个很酷的Feature,谁都可以扩展类的定义,既可以扩展整个类,也可以扩展某个实例,Cool。

扩展类定义 (例子是扩展系统自定义的Array类)

  1. class Array
  2.     def dump
  3.       for x in self
  4.         puts x
  5.       end
  6.     end
  7.   end
  8.  
  9. a = [1,2,3,4,5]
  10.   a.dump

扩展类实例 - Singleton Class

  1. class X
  2.   def f()
  3.     puts "f()!"
  4.   end
  5. end
  6.  
  7. x = X.new
  8. x.f()
  9.  
  10. def x.g()
  11.   puts "g()!"
  12. end
  13.  
  14. x.g()
  15.  
  16. y = X.new
  17. y.f()
  18. # y.g() # Undefined method

Hooks

在你感兴趣的任何地方添加钩子。

其它还有很多,比如:

  • Everything is object
  • Implied self argument
  • Module & Mix-ins
  • … …

Popularity: 24%

Related entries:

3 Responses to “First glance at Ruby”

  1. yuchifang Says:

    def create(klass, value)
    klass.new(value)
    end

    g = create(Greeting, “Hello”)

    加强版的define?

  2. yuchifang Says:

    我是说C语言的#define
    #define create(klass, value)\
    klass.new(value);

    g = create(Greeting, “Hello”)

  3. Meng Yan Says:

    这里的重点应该是 klass.new :-)

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI

 

Creative Commons License
This work is licensed under a Creative Commons License.

一天就好象是这短暂的一生,一生它只是无尽的路上短暂的一天