The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

Posts Tagged ‘tips’

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:

[‘myfriend1′,’myfriend2’]

[‘link’,’text’]

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 http://gist.github.com/78498

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

Advertisements

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

Two of the best help & guide sites for RubyOnRails and Ruby

Posted by arjunghosh on February 27, 2009

Well this is essentially a note to self and well to others too.
The best help documents online for Ruby On Rails and Ruby are these AJAXified websites which allows real-time searching of sytanx and commands.

For Ruby On Rails, it is:

www.railsbrain.com

And for Ruby, we have:

www.rubybrain.com

And the best part is you can even download them so that you can use it offline!

Another good resource for Rails documentaion and help guides is the newly created collaborative effort Rails Guide project. This was started by @lifo as a project to give the rails community a good documentaion which was painfully lacking till now. Since its a collaborative effort, you can participate here. There is even buzz about this project on @twitter

Posted in Uncategorized | Tagged: , , , , , , | Leave a Comment »

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 = “xyz@gmail.com”
vcard = ERB.new( File.open(‘address.vcard.erb’ ){ |f| f.read } ).result( binding )
f = File.new(“temp_file.vcf”,”wb”)
f.puts vcard
f.close
}

and in pastie is here

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

Model name in RubyOnRails should ALWAYS be singular!!!

Posted by arjunghosh on December 23, 2008

Model names in RubyOnRails should be Singular. This will create Table name as corresponding Plural names.

You have got to remember that about model naming conventions in rails. . 

I know this is a very basic concept in RubyOnRails but sometimes these simple basic concepts are the ones which slip through the cracks.

Example:

> ruby script/generate model Post

This will create a Model by the name “Post” i.e. singular in english language and a migration to create a table by the name “Posts” which is plural form of “Post”.

I have even put this into a pastie for easier remembarance http://pastie.org/345491

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