14 May 2006

Syntax memo

  • nil is a object
  • Class is a object, too
  • Variable name and block is not a object
  • Method invokation is sending message to object
  • Module predifined a set of behaviors (methods) for mix-in, it's like interface of Java but with implementation
  • If multiple methods defined with the same name, first matches win. The search path is from buttom to top class and from last to first included modules
  • attr_reader, attr_accesor for getter/setter
  • Variable name in Uppercase is constant
  • @instance_var, $global_var, local_var
  • false for "a".equals?("a"), true for "a" == "a". equals? is for identity equal and should not be overridden
  • Symbol is an instance of Symbol class. It is an immutable, singleton, String-like Object
  • bareword "super" will forward arguments automatically.
  • use :: to navigate nested class/module/Constant, just like file system do. Both relative or absolute (Start with ::) works.
  • self
     puts self         #main
     class C
       puts self       #C
       module M
         puts self     #C::M
       end
       puts self       #C
       def self.my_m   #C
         puts self     #C
       end
       def my_m2
         puts self     #instance of C
         @var          #instance var. of instance of C
       end
       @var            #instance var. of class C
     end
         
  • in instance method, self.foo="bar" invokes setter, but foo="bar" is local var assignment
  • English.rb for predefined global var.
  • Unlike java, access rule "private" works for "self". To access members between instance(same class), use protected instead. protected also works for subclass.
  • public/protected/private denotes a block of codes, use with care.
  • "case when" use === to test match. === is syntax sugar for .===() method call.
  • begin rescue ensure end for try{} catch{} finally{}

Some thoughts

  • Ruby does not support unicode yet. So nobody in Taiwan will concern it seriously. Ruby 2.0 will have built-in unicode support, let's wait...
  • Ruby is so dynamic that compiler can't help you much. Thus write unit test anyway if you are in serious. Don't trust developers who never writing unit test.
  • Ruby is too powerful. Not only providing object oriented programming, but also meta programming. In enterprise, most developers are in average and still not understands OOP well. They write programs in half procedured + half OOP. I don't believe that average developers can master meta-programming well. Bring Ruby to them is just like bring a machine gun without safety lock to a baby. For coporations, a safe, robust, and easier to manage platform is much prefered.
  • Writing program without refactoring is just like using only one hand to coding. I really doubt that how average Ruby programmers can achieve good code quality and high productivity without IDE support.
  • Ruby is not for everyone. It is for smart and top-notch developers. If your team successfully deliver middle-size-project with Ruby. Yeah, you are a real genius and, luckily, works with smart team members.

回響

可以用 Tag <I>、<B>,程式碼請用 <PRE>