Elia Schito

Archive for the ‘Ruby snippets’ Category

Get input in Rake Tasks

In Programming, rake, Ruby, Ruby snippets, Snippets on November 7, 2008 at 2:47 pm

This won’t work:

cvs_username = gets

to get it working you have to ask input from STDIN:

cvs_username = STDIN.gets

And this is a helper method:

def ask message
print message
STDIN.gets.chomp
end
# ...
cvs_username = ask('Please insert your CVS username')

How to declare top rake prerequisites from within a namespace

In Programming, Ruby, Ruby snippets, Snippets, Tutorials on May 28, 2008 at 12:48 pm

Here’s the problem

task :mytask do
  # ...
end

namespace :myscope do
  task :mytask => :mytask
end

This will end up with loop dependency error, so how we can call a top level task with the same name of one from our current scope?

The solution is not documented, i found it directly in the rake’s library code.

task :mytask do
  # ...
end

namespace :myscope do
  task :mytask => "rake:mytask"
end

Rails inflector uncountable troubles? Just remember Why’s Dr. Cham!

In Inflector, pluralize, Programming, Rails, Ruby on Rails, Ruby snippets, Tricks, Tutorials, uncountables, Why's Dr. Cham on July 26, 2007 at 6:00 pm
Inflector.inflections do |inflect|
  #this will do nothing, uncountables have precedence
  inflect.irregular 'equipment', 'equipments'

  def inflect.remove_uncountable(word)
    @uncountables.delete word
  end
  inflect.remove_uncountable 'equipment'
end

Gems through the proxy (or the «undefined method `[]=’» error)

In Gems, Gems proxy, Rails, Ruby, Ruby on Rails, Ruby snippets, Tricks, Tutorials on July 18, 2007 at 7:37 pm

Ok, if you have tried to work with the gem command through a proxy, like gem install rails -p http://proxy.ccci.org:8080 you surely incurred in the following error:

ERROR: While executing gem ... (NoMethodError)
undefined method `[]=' for Gem::ConfigFile

The solution is barely to patch the Gem::ConfigFile class adding the following method:

def []=(key, value)
  @hash[key.to_s]=value
end

(I added it at line 72)
The only hard part colud be to locate that class inside the ruby libraries… don’t worry here’s the path:

ruby/site_ruby/1.8/rubygems/config_file.rb

this is (obviously) relative to the ruby folder, on my MacBook it is located in: /opt/local/lib/

Update: Translation in Portuguese

Update/2: Gems Proxy under windows

Wipe out all Subversion informations with a single line of ruby

In Ruby, Ruby snippets, Subversion, SVN on June 29, 2007 at 11:20 am

From your working copy root, type:

ruby -e '`find . .svn | grep svn`.each{ |file| `rm -rf #{file}` if /\/\.svn\/$/ =~ file }'

Surely there’s some redundancy in this code (and does not work on Windows too), tell me the better solutions.
This is not like svn export. With svn export you get only the files under version control.