Railstic

sharing experiences

Updating RubyGems to 1.2 (Manually)

| Comments


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


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:


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

Recursive methods with block in Ruby

| Comments


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.

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

Output :

lucky luke
jolly jumper
rin tin tin
joe
william
jack
averell

How to check if a div exists in rjs?

| Comments


<body>
   <div id='main'>
      Main Div
   </div>
</body>

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.