Posts for the month of February 2007

Ruby IDE for NetBeans Almost Useful

As NetBeans IDE 6.0M7 released, I tried the Ruby module for it, and it's almost useful now.

To get and install,

1. Downloand NetBeans IDE 6.0M7 from:
http://www.netbeans.info/downloads/dev.php
Select 'Q-Build' and download the newest M7

2. Update Ruby modules:
1) [Tools] -> [Update Center]
2) Select Ruby folder as you wanted (9 files will be selected)
3) Following the instructions.

3. Set your Ruby environment:
As the default installation will use JRuby, if you want to use c-ruby, go to
1) [Tools]->[Options]->Miscellaneous->Ruby Installation
2) Change all ruby tools to yours

4. Now setup your first Ruby on Rails Application:
1) [File]->[New Project]->Ruby->Ruby on Rails Application
2) If you have an existed project, copy and override to the new created project tree.

Want to take a look at the snapshot? here it is:
NetBeans + Ruby = True

That's all. Have fun with great NetBeans.

Notice: If you are using c-ruby, don't try to run project via NetBeans' "run main project" button, which may change your environment temporarily.

AIOTrade: Yahoo! data module updated

Due to the recent date format change in the Yahoo! historical data, I've updated the relative module: org-aiotrade-platform-modules-dataserver-basic.nbm

And also updated module: org-aiotrade-math.nbm to fix the bug mentioned at: Solution for bug: wrong date shown

To update your installed Aiotrade 1.0.3:

1.From menu: Tools -> Update Center
2.In the "Select Location of Modules" pane, click "Install Manually Downloaded Modules(.nbm Files)", then "Next"
3.Click [Add...] button, go to the path to select these two .nbm files that you downloaded. (Press Ctrl key down and click on file name to select multiple files)
4.Following the instructions to install updated modules.
5.Restart Aiotrade.

!Notice, you may need to delete all aiotrade log files, which are located at:
For Windows user:
C:\Documents and Settings\yourusername\Application Data\.aiotrade\1.0.3\var\log\
For Mac user:
${HOME}/Library/Application Support/aiotrade/1.0.3/var/log/

This is a quick fix, the final solution will be included in next release.

Functinal Style Ruby

After playing with Ruby for weeks, I found Ruby is yet interesting. I try to write my code a bit in the likeness of Erlang, where symbol vs atom, array vs list. And the most important syntax that I like are:

  • everything return a value
  • may return multiple values
  • begin-end clause is lambda that may be directly applied
  • parallel assignment

Now, let's write some code before and after (functional):

Example1:

Before

   1. cond = {}  
   2. if par[:id]  
   3.   feed = Feed.find(par[:id])  
   4.   if feed  
   5.     cond[:feed] = feed.id  
   6.   end  
   7. end  
   8. if par[:m]
   9.   limit = par[:m].to_i  
  10. else  
  11.   limit = 20  
  12. end  
  13. if limit >= 4096  
  14.   limit = 4096  
  15. end  
  16. cond[:limit] = limit  
  17. if par[:d]  
  18.   days = par[:d].to_f  
  19.   if days <= 0 || days >= 365  
  20.     days = 365  
  21.   end  
  22.   cond[:time] = Time.now - days*86400  
  23. end  
After
   1. cond = {  
   2.   :feed   => if par[:id]  
   3.                feed = Feed.find(par[:id])  
   4.                feed ? feed.id : nil  
   5.              end,  
   6.   :limit  => begin  
   7.                 limit = par[:m] ? par[:m].to_i : 20  
   8.                 limit >= 4096 ? 4096 : limit  
   9.              end,  
  10.   :time   => if par[:d]  
  11.                days = par[:d].to_f  
  12.                days = days <= 0 || days >= 365 ? 365 : days  
  13.                Time.now - days * 86400  
  14.              end,  
  15. }.delete_if { |k, v| v.nil? } # delete all nil elements of cond  

Example2:

Before

   1.  if f[:mode] == "rss"  
   2.   rss = f[:feed]  
   3.   params[:feed][:channel] = rss.channel.title  
   4.   params[:feed][:description] = rss.channel.description  
   5.   params[:feed][:link] = rss.channel.link  
   6.   params[:feed][:copyright] = rss.channel.copyright  
   7. else  
   8.   atom = f[:feed]  
   9.   params[:feed][:channel] = atom.title  
  10.   params[:feed][:description] = atom.subtitle  
  11.   params[:feed][:link] = atom.links.join  
  12.   params[:feed][:copyright] = atom.rights  
  13. end  
After
   1. params[:feed][:channel],    
   2. params[:feed][:description],    
   3. params[:feed][:link],    
   4. params[:feed][:copyright] = if f[:mode] == "rss"    
   5.                               rss = f[:feed]   
   6.   
   7.                               [rss.channel.title,    
   8.                                rss.channel.description,    
   9.                                rss.channel.link,    
  10.                                rss.channel.copyright]  
  11.                             else    
  12.                               atom = f[:feed]  
  13.     
  14.                               [atom.title,    
  15.                                atom.subtitle,    
  16.                                atom.links.join,    
  17.                                atom.rights]    
  18.                             end    

Example3

   1. # grp_str: p -> public(0) , u -> user(1), f -> friends(2)   
   2. def privilege_cond(user, grp_str)  
   3.   grp_str ||= 'puf'  
   4.   cond = {:pre => "", :sub => []}  
   5.   cond = if loggedin?(user)  
   6.            frds = grp_str.include?('f') ? user.friends.find(:all) : []  
   7.            frd_ids = frds.collect { |frd| frd.friend_id.to_i }  
   8.              
   9.            cond = if grp_str.include?('u')  
  10.                     {:pre => cond[:pre] + (cond[:pre] == "" ? "" : "OR") +   
  11.                              " user_id  = ? ",  
  12.                      :sub => cond[:sub] + [user.id]}  
  13.                   else  
  14.                     cond  
  15.                   end  
  16.       
  17.            cond = if grp_str.include?('f') && !frd_ids.empty?  
  18.                     {:pre => cond[:pre] + (cond[:pre] == "" ? "" : "OR") +   
  19.                              " user_id in (?) AND privilege in (?) ",  
  20.                      :sub => cond[:sub] + [frd_ids, [0, 2]]}  
  21.                   else  
  22.                     cond  
  23.                   end  
  24.       
  25.            cond = if grp_str.include?('p')  
  26.                     {:pre => cond[:pre] + (cond[:pre] == "" ? "" : "OR") +   
  27.                              " user_id != ? AND privilege  = ? ",  
  28.                      :sub => cond[:sub] + [user.id, 0]}  
  29.                   else  
  30.                      cond  
  31.                   end  
  32.          else  
  33.            {:pre => "privilege = ?",  
  34.             :sub => [0]}  
  35.          end  
  36. end