27

Aug

Updating RubyGems to 1.2 (Manually)

This is what happened when I want to install rmagick gem.

1
2
3
username@username-desktop:~$ sudo gem install rmagick
Bulk updating Gem source index for: http://gems.rubyforge.org/
ERROR:  could not find rmagick locally or in a repository

What the hell…
After some googling I found that RubGems 1.1.x is buggy and and update to RubyGems 1.2 is necessary.
To learn your RubyGems version:

1
2
username@username-desktop:~$ gem -v
1.1.0

I try to update RubyGems :

1
2
3
4
username@username-desktop:~$ sudo gem update --system
Updating RubyGems
Bulk updating Gem source index for: http://gems.rubyforge.org/
Nothing to update

After this failed attempt, I realized that RubyGems 1.1.x is really buggy. So I must go on manually to update RubyGems.

Now the solution:

  1. Download rubygems-update-1.2.0.gem
  2. Change your directory where your downloaded gem is. In these example my gem is at Desktop.
    Now you must install rubygems-update-1.2.0.gem.

    1
    2
    
    username@username-desktop:~$ cd Desktop/
    username@username-desktop:~/Desktop$ sudo gem install rubygems-update-1.2.0.gem
  3. 1
    
    username@username-desktop:~$sudo update_rubygems

To check if our process is successful :

1
2
username@username-desktop:~$gem -v
1.2.0

If you see 1.2.0, you did it.

26

Aug

Recursive methods with block in Ruby

1
2
3
4
5
6
7
8
9
def comic(cast)
   cast.each do |character|
      unless character.is_a?(Array)
         yield(character)
      else
	 comic(character) {|x| yield x}
      end
   end
end

This method takes an array as argument and checks each element if it is an Array.
If the element is not an Array then the block is called.
If the element is an Array then the same method is called with that element as an argument and with the same block.

Now let’s look how we can call this method.

1
2
names = ['lucky luke', 'jolly jumper', 'rin tin tin', ['joe', 'william','jack', 'averell']]
comic(names) {|c| puts c}

Output :

1
2
3
4
5
6
7
lucky luke
jolly jumper
rin tin tin
joe
william
jack
averell

24

Aug

How to check if a div exists in rjs?

?View Code HTML4STRICT
1
2
3
4
5
<body>
   <div id='main'>
      Main Div
   </div>
</body>

1
2
3
4
5
render :update do |page|
   page << "if ($('main')){"
   page.replace_html 'main', :inline => 'Here it is'
   page << "}"
end

This ruby code checks if there is a DOM object that its id = “main”.
If there is then changes its content to “Here it is”, if there is not do nothing.