Home

Ruby namespace tips

After made below test, then you don’t need to use Test::B.bbb in everywhere of your plugin, just use B.bbb.

class A
  def self.aaa
    puts "a"
  end
end

module Test
  class A
    def self.aaa
      puts "aaaaaaaaaa"
    end
  end
end

module Test
  class B
    def self.bbb
      A.aaa
    end
  end
end


Test::B.bbb # => aaaaaaaaaa
A.aaa # => a

Found a way to be more productive

Personally, I like to focus on doing one thing and make it perfect, so as soon as I been engaged to one thing, I will spend all my time and consentration on it.

Then How to engage one project as soon as possible?

  1. Stop doing other projects, or phase out doing them.
  2. To know more things related to this project.
  3. Clarify the aim and things you need to do, seems like couple of days job could finish them. (that’s why we need a sprint, if it’s too long, it’s frustring.)
  4. Make the first success ;)

ActiveSupport autoload_at

What does below code means?

autoload_at "action_view/template/resolver" do
   autoload :Resolver
   autoload :PathResolver
   autoload :FileSystemResolver
   autoload :OptimizedFileSystemResolver
   autoload :FallbackFileSystemResolver
end

Pretty simple, it will load all Modules in that block in action_view/template/resolver.rb file.


How to send Multipart email in Rails.

Yes, it sounds pretty easy, just write code like below:

mail(:to => user.email,
     :subject => "Welcome to My Awesome Site") do |format|
   format.html
   format.text
end

This code will generate a multipart email, that put the html content as the first part, and text content as second part. This will cause a problem is, when you open the email in mailbox, Oh, 奶奶的, why it’s text version. You must expect “It should show html when mail client support MIME, and show text version if it’s not support MIME.”

Actually, the truth is:

Before the first boundary is an area that is ignored by MIME-compliant clients. This area is generally used to put a message to users of old non-MIME clients.


RVM install REE got error on Lion

Today I install json gem, got a an error, then googled a little bit found I need to reinstall my ruby in RVM, then I updated rvm and reinstalled ruby 1.9.3, but got problem when install REE, got following errors:

* Non-broken C compiler... not found 
* Non-broken C++ compiler... not found 
* The 'make' tool... found at /usr/bin/make 
* The 'patch' tool... found at /usr/bin/patch 
* Zlib development headers... found 
* OpenSSL development headers... found 
* GNU Readline development headers... found

After I installed the osx-gcc-installer

Then things solved.

Since Lion is using LLVM-gcc instead of gcc, so it can’t compile REE.