themactep

a collection of miscellaneous projects of Paul Philippov

Ruby Useful Tips

How to lookup Rubygems path

$ ruby -r rubygems -e "puts Gem.path"

or look it up in the environment

$ gem env

How to update gems for multiple projects at a time

$ for d in $(ls -1 -d */); do cd $d; pwd; bundle up; cd ..; done

How to define ruby 1.9 alternatives on Debian/Ubuntu

$ sudo -s
# apt-get install ruby1.9.1 ri1.9.1
# update-alternatives \
    --install /usr/bin/ruby ruby /usr/bin/ruby1.9.1 400 \
    --slave /usr/share/man/man1/ruby.1.gz ruby.1.gz /usr/share/man/man1/ruby1.9.1.1.gz \
    --slave /usr/bin/ri ri /usr/bin/ri1.9.1 \
    --slave /usr/bin/irb irb /usr/bin/irb1.9.1 \
    --slave /usr/bin/rake rake /usr/bin/rake1.9.1 \
    --slave /usr/bin/rdoc rdoc /usr/bin/rdoc1.9.1 \
    --slave /usr/bin/gem gem /usr/bin/gem1.9.1 \
    --slave /usr/bin/erb erb /usr/bin/erb1.9.1
# exit

How to update RubyGems in Debian

$ sudo gem install rubygems-update
$ sudo update_rubygems

How to create an app with a particular verions of Rails

$ rails _4.7.0_ new appname

How to uninstall a particular version of Rails in one shot

$ sudo gem uninstall {rails,railties,activemodel,activerecord,activeresource,\
activesupport,actionmailer,actionpack} -v 3.0.6

How to shut up Webrick warnings about content-length

$ sudo sed -i "205,205 {/^\s*if chunked\?/s/$/ || @status == 304 || @status == 204/}" \
  /usr/lib/ruby/1.9.1/webrick/httpresponse.rb

Based on solution from http://stackoverflow.com/a/9071474

How to auto-reload Sinatra project on every request*

$ touch tmp/always_restart.txt
  • — only if you use Phusion Passenger

How to make an application to include local libraries

$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))

How to get Ruby date and time in RFC3339 format

Time.now.xmlschema

How to validate feed URL in Rails

validates :feed_url, format: {with: /^(#{URI::regexp(%w(feed http https))})$/}

How to check if application has enough privileges

raise 'Must run as root' unless Process.uid == 0

How to check if at least one object in array has a true boolean property

@facts.map(&:is_proven?).include?(true)

How to run standalone Passenger for Rails app in production mode

$ passenger start -a 0.0.0.0 -p 3000 -e production