Elia Schito

Archive for the ‘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
# ...
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
  # ...

namespace :myscope do
  task :mytask => :mytask

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
  # ...

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

rails runner… with a relative path

In Programming, Rails, Ruby, Ruby on Rails, Snippets on January 23, 2008 at 10:43 am

Well, you know what ./script/runner is?

If so, you’ll know you can’t use it to build script without using an absolute path in the shebang line in your custom scripts:

> ./script/runner -h
Usage: ./script/runner [options] ('Some.ruby(code)' or a filename)
You can also use runner as a shebang line for your scripts like this:
#!/usr/bin/env /path/to/your/app/script/runner

 So here’s the solution! (here the script is saved in the script dir)

#!/usr/bin/env ruby
unless $0 =~ /runner/
  exec("#{File.dirname(__FILE__)}/runner #{__FILE__}")
  # "exec" exits the current processing, so this comment won't reached (thanks to Arthaey Angosii)
# Your great script goes here!


#!/usr/bin/env ruby
$: << File.dirname(__FILE__)
ARGV[0] = __FILE__
load 'runner'
#your code here

Snippets: svn:ignore and patch

In Ruby on Rails, Snippets, Subversion, Subversion patch, SVN, svn:ignore, Tutorials on September 23, 2007 at 6:01 pm

Every time the same problem: you’re at the command line and you need to make Subversion ignore some files, so here’s the snippet:

svn  propset svn:ignore "ruby_sess.*" ./tmp/sessions/

Even more rare: creating patches: see this post