Transform Matching Text with Gsub in Ruby and Regular Expression
Here is a gist that demonstrates how easy it is to transform text using #gsub and a block with Ruby.
For more helpful string extensions in Ruby check out our Ruby Gem on GitHub https://github.com/AgilionApps/rails_extensions
ruby Ruby on Rails: autotest color hacks ruby tdd testing tools workarounds workflow
by bseanvt
1 comment
Color Output with Test:Unit, AutoTest and Ruby 1.9
If you are testing using Test:Unit (rather than RSpec) and you’re using Ruby 1.9.* colorized output of your tests using Autotest will not be immediately available. Since, 1.9 comes with mini test the test/unit/ui/console/testrunner.rb script is not loaded and not available and will break your tests.
The solution is to require Test:Unit version 2.0.0 in your Gemfile, require the testrunner.rb script in test/test_helper.rb and reopen and implement the guess_color_availability method.
Then you can just run the autotest command (or bundle exec autotest) from your project directory. When you save a file your tests will be run for the file that has been changed and the results will be fully colorized!
ruby: data structures hash how to programming ruby yaml
by bseanvt
leave a comment
Class << Self Explained
# Define a class with a class method "find"
# Usage
# Apple.find("macintosh")
class Apple
def self.find(variety)
# code goes here
end
end
# Same as above but notice the lack of self prefix before the method name
# Usage
# Apple.find("macintosh")
class Apple
class << self
def find(variety)
# code goes here
end
end
end
See the full pastie here: http://pastie.org/2580020
How to Upgrade RVM on Mac OS X
I had an old version of rvm installed and wanted to upgrade. So old in fact that the resource for upgrading no longer existed.
rvm update
just returned a 301, redirect.
Luckily, the following worked
# checks out from repo rvm update --head # will reload rvm environment rvm reload # finally, the upgrade command works! rvm get latest
ruby: console development environments irb rc files ruby
by bseanvt
leave a comment
Ruby Reload! Method in Non Rails IRB Sessions
I love the Rails reload! function when in the console. I need it in Irb. To get it back this is what I did. If you don’t already have an .irbrc file in your home directory, just create it.
vim ~/.irbrc or textmate if you prefer mate ~/.irbrc
Add this little snippet to it…
unless defined?(reload!)
$files = []
def load!(file)
$files << file
load file
end
def reload!
$files.each { |f| load f }
end
end
Usage : To load files in irb just use the method we defined in the .irbrc file “load!” notice the bang “!”. I don’t want to overwrite the actual load method. This load! method will just put the file in an array before loading it, so when running reload! it will iterate over this collection and load them again with whatever changes have since taken place. The unless conditional is so that you don’t overwrite the reload! method if you’re actually in the rails console.
Ruby Rand Range
I assumed that rand would take a range as an argument. Something like rand(10..20), generating a random number between 10 and 20. Seems like you’d do this fairly often when working with random numbers and therefore, included. However, it doesn’t work. But the solution is almost as easy.
10 + rand(11) #=> produces a random number between 10 and 20
Since rand starts at 0 (like array indexes), we need to add an extra 1 to get what we’re expecting.
Linux ruby Ruby on Rails: console irb Linux passenger production Rails ruby enterprise edition rubygems ubuntu
by bseanvt
leave a comment
Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time
./script/console production # => Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org
You then scratch your head and run
which gem which ruby which rails
to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.
The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…
rm /usr/bin/irb
And symlink it to the irb that REE has in bin
ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb
Now cd into your rails app and run
./script/console production
mac os x ruby: development environment ruby rvm setup version management
by bseanvt
17 comments
Installing and Using Rvm on Mac OS X, Creating Gemsets and Reverting to Original Environment
What is RVM and why should you use it? RVM is a Ruby interpreter, version management tool. In short, it enables you to switch between different versions and releases of Ruby (for instance, version 1.8.6, 1.8.7, jruby 1.9.2, ruby enterprise edition) on the same machine, while associating different gems with each version of the ruby interpreter. This is super useful and awesome. If you want to play with Rails 3 and Ruby 1.9.1, for 5 minutes, and then want to switch back to your production apps, which are running on Rails 2.3.5 and Ruby 1.8.7, you can do so with a single command from the terminal. With RVM this is a fairly simple process so there is no reason not to install it. You can also revert back to your system settings (not using RVM) with a single command. After all Rails is just a gem, so you can easily create and manage different RVM “gemsets”, (sets of different gems), for the different versions of Ruby (rubies as RVM refers to them) you have installed.
Installing RVM
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
Next you have to add rvm to your bash profile
# place in ~/.bash_profile as the very last line [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
To check everything went well
type rvm | head -n1
Should tell you “rvm is a function”
How to add ruby, pass it the version to install
rvm install 1.8.7
*The current terminal session will load this environment. New sessions will not. To use a version of ruby and set it as the default, pass it the –default option
rvm use 1.8.7 --default
Next create a gemset, which will make available different gems for different versions
rvm gemset create rails_2_3_5
When you run “gem list”, you should see nothing!
gem install rails -v=2.3.5
Set a default rvm and default gemset, specify which gemset with the @ sign and include the –default option
rvm use 1.8.7@rails_2_3_5 --default
which gem gem list ruby --version rails --version
And to get back to where you started and revert to using your original ruby setup
rvm system
For upgrading your version of RVM check out this post I wrote http://seanbehan.com/ruby/how-to-upgrade-rvm-on-mac-os-x/
Finally, you can create a .rvmrc file and put it in any directory and when you cd into that directory the environment specified in the file will be loaded automatically. This way you don’t have to remember the version and gemsets and type them into the console. All you have to do is put the ruby version and gemset name in the file like so
ruby1.8.7@rails2.3.5
You’ll be prompted to trust the .rvmrc file the first time, type “y” for yes. Also, subdirectories will inherit this .rvmrc so you can just put it in the parent directory like
rails2/
.rvmrc
app1
app2
rails3/
.rvmrc
app1
app2
And both app1 and app2 will use the .rvmrc environment while your rails3 directory apps will load the environment in its directory!
More information available here:
http://rvm.beginrescueend.com/rvm/install/
http://www.stjhimy.com/posts/4
http://eddorre.com/posts/installing-rails-3-beta-4-using-rvm
Combat Spam with the Akismet Class for Ruby
Here is the Akismet.rb class, written by David Czarnecki. I’ve seen several tutorials online using this class, however, the class isn’t available at David’s blog. So… I reposted it here and at the pastie link below
http://pastie.org/1150693


