The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

Posts Tagged ‘hacks’

How to parse a tweet text from Twitter using Ruby to parse-out ‘@’ and ‘#’

Posted by arjunghosh on March 5, 2009

Well lot of us love @twitter and also Ruby, and some time work on both 🙂

And often we need to do the folowing with a tweet

Well I had to do the following quite often:-

Take out the ‘@’ (i.e. @replies )and ‘#’ (i.e. hashtags ) from a tweet and separate it from the text part.

For example, we have a tweet:

@myfriend1 @myfriend2 this is a sample text #link #text

Now I want this tweet to be seperated into the following Array:



and the text only – [“this is a sample text “]

So first had to build a RegE, and then using the ever useful .gsub method of Ruby, created the following:

parsed_text = tweet.text.gsub(/ ?(@\w+)| ?(#\w+)/) { |a| ((a.include?(‘#’)) ? tags : replies) << a.strip.gsub(/#|@/,”); ” }

So the parsed_text has the final text only.  tags is an Array which will contain the hashtags and replies is an Array which will contain the @replies.

The RegEx / ?(@\w+)| ?(#\w+)/ extracts and seperates the hashtags & the @replies and place them in two seperate arrays.

The RegEx /#|@/,” only reples the ‘@’ and ‘#’ symbols in the extracted array elements.

And you can download it from Gist here

Also while working on creating the above regular expressions, I found this interesting RegEx testing site called which will help you write regular expressions very easily.


Posted in Uncategorized | Tagged: , , , , , , | 8 Comments »

Using ERB as a dynamic template to create a file whose contents are dynamic

Posted by arjunghosh on February 17, 2009

The following code will create a temporary file for say attaching a vcard file to email.The following code is dynamic to use ERB as a template to create the file dynamically every time with the variable “telephone”,”location”,”email” which gets binded to the ERB file and is under scope.
The code is:-

telephone = “347-27456”
location = “New Delhi”
email = “”
vcard =‘address.vcard.erb’ ){ |f| } ).result( binding )
f =“temp_file.vcf”,”wb”)
f.puts vcard

and in pastie is here

Posted in Uncategorized | Tagged: , , , , , , , | 1 Comment »

Some changes in Rails2.0

Posted by arjunghosh on January 7, 2008

This post is mostly as a reminder to self. There are some changes in Rails 2.0:-

1. SQLite3 is the new default database. So when a rails app is created, by default the database support, which comes preconfigured, is for SQLite3. Though I am not sure how popular this move by Rails core will be, since most of(like me) us have been using MySql for long and it has some excellent support tools. So anyways, if you still want to create a rails app with MySql support preconfigured, you simply need to do

rails -d mysql myapp

and everything is the same as before. “-d” is for preconfiguring selected database. So above command will work for any database adapter.

2.The command to generate RESTful scaffold –

script/generate scaffold_resource model_name

no longer works. Instead, you have to do this:

script/generate scaffold model_name

3. In RESTful scaffold, the view files for the various controller actions were given file names such as “edit.html.erb” instead of “edit.rhtml”. The reason is because “.rhtml” has been deprecated in Rails 2.0.

A good link I found on Rails 2.0 changes was by Brad

Of course there is the DHH blog post :).

Posted in Uncategorized | Tagged: , , , | 1 Comment »

Excellent rake task code snippet to completely rebuild & migrate your db

Posted by arjunghosh on January 4, 2008

The other day I came across a code snippet which completely automates the re-building and migrating of database. It also run all my rake task for db which I can include in it. All this is done by single line of code. I thought I will share it here.

1. First you need to create a rake file in your /lib/tasks folder of your Rails application. Say it is called


2. In this file, add all your customized rake tasks for your db like adding initial seed data to db,etc.

3. Then add the following code just before the end of the rake task :

desc ‘rebuild the database from scratch’

task :rebuild=> :environment do
p “Beggining the rebuild”

desc ‘destroy your database’

task :drop_and_create=> :environment do
db_name = ActiveRecord::Base.connection.current_database
p “Dropping the database”
p “Creating the database”
db_config = YAML.load_file(File.join(RAILS_ROOT, “config”, “database.yml”))

Now whenever you need to re-create your db and run all your migrations and run all your tasks for db, you need not go and run multiple commands like db:migrate, then db:your_rake_tasks, etc.

You can simply run the following:

rake db:rebuild

🙂 cool right.

Posted in Uncategorized | Tagged: , , , | 2 Comments »

db:session:create does not seem to work for creating Sessions table

Posted by arjunghosh on January 4, 2008

Well we all know when have multiple machines accessing the same session information, we need need to store the session information somewhere so that all the servers can access it. The solution is store the session information in a database and access the session information from the database.

So with that purpose in mind, I searched some rails books and found that rails has a way to putting sessions in the database.

rake db:session:create

So went ahead an entered this command at my console for my application. And lo behold, what has happened!!!

I get the following error:

(in F:/testdev/trunk)
rake aborted!
Don’t know how to build task ‘db:session:create’
(See full trace by running task with –trace)

So I checked the rake –help and also googled it. It seem lot of them have the same error. In fact it seem that AWS 2nd book also has this error as per an excellent writeup in a blog.

And here I found the solution:

rake db:sessions:create

The issue was with a single ‘s’. Now I use a rake version 0.7.3. Is that the problem? Am I using a lower version of rake?

Posted in Uncategorized | Tagged: , , , , , | 1 Comment »