Monday, 29 October 2012

How to make an alias in the terminal on mac osx

Credits to:

http://highervisibilitywebsites.com/how-make-terminal-alias-mac-os-x

After looking around for how to make an alias for Mac OS X's terminal/shell I ended up cobbling together my solution from a variety of different (mostly unixy-linuxy) places. So in the name of good documentation, here is the magic formula for the next time I need to set up an alias for happier command line hacking:
  1. First we edit/create a profile. For a normal user do:
    pico /etc/profle
    ...or for root/superuser do:
    pico ~/.profile
  2. Add your alias like so to the file:
    alias aliasname='mycommand /path/path'
    (notice no space between equal sign and ')
  3. Save your changes and close the file
  4. Load/reload your profile with:
    . /etc/profile
    ...or for root/superuser do:
    . ~/.profile
  5. If you are using root/sudo you will need to use sudo -i in order to load the profile upon login (more info about this here).
  6. Done.

highlight effect without jquery-ui

So i recently had to do a highlight effect without the use of jquery-ui. Something like this:
http://docs.jquery.com/UI/Effects/Highlight

jQuery.fn.highlight = function(level) {

  highlightIn = function(options){

    var el = options.el;
    var visible = options.visible !== undefined ? options.visible : true;

    setTimeout(function(){
      if (visible) {
        el.css('background-color', 'rgba('+[255, 64, 64]+','+options.iteration/10+')');
        if (options.iteration/10 < 1) {
          options.iteration += 2;
          highlightIn(options);
        }
      } else {
        el.css('background-color', 'rgba('+[255, 64, 64]+','+options.iteration/10+')');
        if (options.iteration/10 > 0) {
          options.iteration -= 2;
          highlightIn(options);
        } else {
          el.css('background-color', '#fff');
        }
      }
    }, 50);
  };

  highlightOut = function(options) {
    options.visible = false;
    highlightIn(options);
  };

  level = typeof level !== 'undefined' ? level : 'warning';
  highlightIn({'iteration': 1, 'el': $(this), 'color': level});
  highlightOut({'iteration': 10, 'el': $(this), 'color': level});
};

Friday, 26 October 2012

rails debugger

So i recently discovered a debugger for ruby on rails. Since i've been using emacs there isn't a lot of options. Luckily we have the debugger gem which is very useful:

CREDITS TO:

http://www.intridea.com/blog/2010/12/7/debug_rails_application_with_ruby-debug

I worked with GNU C/C++ for a long time before I met Ruby. The two languages are very different; they have their own strengths and weaknesses in different application scenarios. But there is one nice thing that they have in common: they both have great debuggers for their developers who are hitting their heads on their keyboards trying to find out what has gone wrong with their applications.
My associate, Yong Zhi, wrote a great blog post on how to debug a Ruby application, a standalone script, or a full-stacked Rails application with GDB, GNU Project Debugger. Within the Ruby community there is always more than one option you can choose from. As many of you know, ruby-debugprovides almost the same directives with GDB, so, for the Ruby developers who have used GDB before, there will not be a steep learning curve. What I want to share with you today is how I make my life easier with help from ruby-debug.
The following steps are used to get ruby-debug working with a Rails 3 application.
Step 1: Install ruby-debug
Open Gemfile, add the following lines to it:
group :development do
  gem 'ruby-debug'
end
and run bundle install to install the gem.
Step 2: Start the server for development
rails server --debugger
Step 3: Find the bug
For the line you where you want to setup a breakpoint, just place a 'debugger', whether it's in a view, a controller or a model.
# in a view file new.html.erb
<%= content_for_the_main %>
<% debugger %>
<%= content_for_the_side_column %>

# in a controller file, posts_controller.rb
class PostsController &lt; ApplicationController
  ...

  def new
    debugger
    post = current_user.posts.new
  end

  ...
end

# in a model file, post.rb
class Post &lt; ActiveRecord::Base
  ...

  def self.funny_ones
    debugger
    self.where(:funny => false)
  end

  ...
end
Send another request after setting up the breakpoints; the server will stop at the first breakpoint it runs into, waiting for the further instructions:
(rdb:1)
With the prompt, you can get a list of directives with "help", or "help backtrace" for the usage of specific directive. Usually, I'll use 'l', short for 'list', to take a look at where the application stops:
(rdb:1) l
22    def new
23      debugger
=> 24      post = current_user.posts.new(params[:post])
After that, check out the value of the variable with 'p', short for 'print',
(rdb:1) p params.inspect
After that, you can continue to the next line with 'n', short for 'next',
(rdb:1) n
Or, you can use 'c', short for 'continue', to get to the next breakpoint. If there are not any breakpoints left, the reponse will be sent back to the client and the request-response loop is over.
(rdb:1) c
If you want to know how you got where you are, just call 'bt', short for 'backtrace',(rdb:1) bt
--> #0 TabsController.edit 
      at line /home/hao/Sandbox/demo/app/controllers/posts_controller.rb:24
    #1 Kernel.send_action 
      at line /home/hao/.rvm/gems/ree-1.8.7-2010.02@demo/gems/actionpack-3.0.0/lib/action_controller/metal/implicit_render.rb:4
    #2 ActionController::ImplicitRender.send_action 
      at line /home/hao/.rvm/gems/ree-1.8.7-2010.02@demo/gems/actionpack-3.0.0/lib/action_controller/metal/implicit_render.rb:4
    #3 AbstractController::Base.process_action(method_name#String) 
      at line /home/hao/.rvm/gems/ree-1.8.7-2010.02@demo/gems/actionpack-3.0.0/lib/abstract_controller/base.rb:150
If you want to step into a function call you may have interest in, you can call 's', short for 'step',
(rdb:1) s
To step out of the function call you just stepped into, use 'fin', short for 'finish',
(rdb:1) fin
To quit the debugger, type 'q', short for 'quit',
(rdb:1) q
Those are most of the ruby-debug directives that I find myself using daily. There's one more feature of ruby-debug that can bring great help for the debugging: irb session.
(rdb:1) irb
You can open as many irb sessions as you want, you can change the value of the variable to try it out.
ree-1.8.7-2010.02 > params[:post] = {:title => "Hello, world!", :body => "Hello, world, again!"}
ree-1.8.7-2010.02 > exit
(rdb:1) c 


Thursday, 25 October 2012

jquery select element by the href link

I use this quite often. how to select an link element using the href link:

http://stackoverflow.com/questions/303956/jquery-select-a-which-href-contains-some-string

$('a[href$="ABC"]')...

Yap that's it.