Friday, November 27, 2009

Passing commandline parameter (arguments) to ruby file using optparser

by sandipransing 0 comments
Ruby file accepts from command prompt in the form of array.
Passing parameters
ruby input.rb TOI DH TimesNew
Accessing parameters
 # input.rb
 p ARGV
 # => ["TOI", "DH", "TimesNew"]
 p ARGV[0]
 # => "TOI"
 p ARGV[1]
 # => "DH"
Optparser : parses commandline options in more effective way using OptParser class. and we can access options as hashed parameters.

Passing parameters
ruby input.rb -P"The Times of India" --category"article"
Accessing parameters
 
 # input.rb
 require 'optparser'
 
 options = ARGV.getopts("P:", 'category')

 p options
 # => { 'P' => 'The Times of India', :category => 'article' }

 p options['P']
 # => "The Times of India"
Read More…

Thursday, November 26, 2009

How to get all associated models of rails model

by sandipransing 0 comments
I have Site Model and i wanted to find all associated models of Site model which are having belongs_to relationship. After doing lot headache, here is the solution i found


has_many models of Site Model
 Site.reflect_on_all_associations.map{|mac| mac.class_name if mac.macro==:has_many}.compact
=> ["Layout", "User", "SubmenuLink", "Snippet", "Asset"]

belongs_to model of Site Model
Site.reflect_on_all_associations.map{|mac| mac.class_name if mac.macro==:belongs_to}.compact
=> ["User", "Page", "User"]

To get all associations
Site.reflect_on_all_associations.map{|mac| mac.class_name}.compact
=> ["Layout", "User", "Page", "User", "SubmenuLink", "User", "Snippet", "Asset"]

Hari, U rocks !
Read More…

Tuesday, November 24, 2009

Cricket challenge flash game

by sandipransing 0 comments
Advanced cricket flash game





Read More…

Flash games

by sandipransing 0 comments
3rd worm game ----------------------
  Racing Game ----------------------
 
Read More…

Gravity Master Game Challenge

by sandipransing 0 comments
Collect all rotating circles with the black ball. You can move the ball by drawing shapes with mouse and using physics. The ways of completing a level is only limited by your imagination and line limit. Complete 15 levels to unlock level editor. I recommend to lower your mouse sesitivity. P.S. This is the first game I ever made

Read More…

Monday, November 23, 2009

Upgrading openoffice in ubuntu interpid

by sandipransing 0 comments
Follow simple steps defined in this post for smooth upgrade of openoffice in linux.

click here
Read More…

bundling gems in rails application

by sandipransing 0 comments
Gem Bundling is basically used to maintain all required gems at your application level. It downloads necessary gems and maintain it under "/vender/gems" directory. Its very easy to use gem bundle.
1. Insatll gemcutter gem ( Its gem hosting )
gem install gemcutter
2. Install bundler ( Its a tool that manages gem dependencies for your ruby application. )
gem install bundler
3. Start using
gem bundle
Read More…

Wednesday, November 18, 2009

Get hand over VI / VIM editor

by sandipransing 1 comments

Movements
b   previous word
w   next word
e        end of word
0/^      begining of line
$        end of line
G        end of file
1G/gg    begining of file
/pattern  search next
?pattern  search previous
n         repeat    search forword ( i.e next occurence )
N         repeat    search backword
:line     goto line specified

  *****Modes****
  i   insert mode
  r   replace mode
  s   delete character under cursor and eneter insert mode

  *****Delete******
  x    delete character under cursor
  dd   delete current line
  line dd delete number of lines specified

  ****copy********
  yy        copy current line
  pp        print copied contents
  line yy   copy number of lines specified

  *******visual******

  v     enter visual mode
  aw    highlight word
  as    highlight sentence
  ap    highlight paragraph
  ab    highlight block

  ******undo/redo******
  u       undo
  cntrl+r redo

  ******autocomplete****
  cntrl+x   enter completion mode
  cntrl+p   display autocomplete options

  ******uppercase/lowercase*****
  guu   lowercase line
  gUU   uppercase line

  *******Regx replace****

  range s/foo/bar/arg - replace foo with bar in ‘range’ with

  Values of 'range':
  %               whole file
  number          that particular line
  none            apply to current line only

  values of 'arg':
  none  apply to first occurrence
  g     global (all occurrences)

Select/Macros
  qchar      start recording macro storing it in register ‘char’
  q          end recording
  @char      replay the macro stored in ‘char’
  :1,10 norm! @char run the macro stored in ‘char’ over the 1-10 line range

Read More…

Tuesday, November 17, 2009

YAML file configuration in ruby & rails

by sandipransing 0 comments
While coding in ruby and rails, we often requires variables to be initialized that can be used across application. There are many ways to define configuration variables

1. Initialize variables inside environment file
  # This agencies can be used across application
  AGENCIES = ['TIMES NEWS NETWORK', 'AGENCIES', 'AFP', 'PTI']

  # Configuration for html nodes

  Article_title_tag = "h1.heading"
  Date_auth_agency_tag = "span[@class='byline']"
  Location_content_tag = "div[@class='Normal']"
2. Inside config/initializers/config.rb. Just make config.rb inside initializers and add variables to it.
  #config/initializers/config.rb
  # This agencies can be used across application
  AGENCIES = ['TIMES NEWS NETWORK', 'AGENCIES', 'AFP', 'PTI']

  # Configuration for html nodes
  Article_title_tag = "h1.heading"
  Date_auth_agency_tag = "span[@class='byline']"
  Location_content_tag = "div[@class='Normal']"
3. But most efficient way to organize variables is YML file.
# config/config.yml
the_times_of_india:
  article_title_tag: h1.heading
  date_auth_agency_tag: span[@class='byline']
  loc_content_tag: div[@class='Normal']
Accessing YML files
 # config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
=> true
>> APP_CONFIG
=> {"the_times_of_india"=>{"article_title_tag"=>"h1.heading", "loc_content_tag"=>"div[@class='Normal']", "date_auth_agency_tag"=>"span[@class='byline']"}}
>> APP_CONFIG[:the_times_of_india]
=> nil
>> APP_CONFIG['the_times_of_india']
=> {"article_title_tag"=>"h1.heading", "loc_content_tag"=>"div[@class='Normal']", "date_auth_agency_tag"=>"span[@class='byline']"}
>> APP_CONFIG['the_times_of_india']['article_title_tag']
=> "h1.heading"
Read More…

Sunday, November 15, 2009

pidgin install and update pidgin messenger on ubuntu interpid

by sandipransing 0 comments
Fresh pidgin Installation
 sudo apt-get update
 sudo apt-get install pidgin
Earlier versions of pidgin has problem in connecting yahoo messenger which is solved in newer versions.
In order to update old version of pidgin, simply follow instructions written below and you are done.
New version provides video and voice chat, also buddy icon improvements are added.

Add GPG key
 sudo apt-get update
 sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A1F196A8
Goto System > Administration > Software Sources and select Third-Party Software tab and click ADD. And Simply Copy-Paste the following Repo
deb http://ppa.launchpad.net/pidgin-developers/ppa/ubuntu intrepid main 
deb-src http://ppa.launchpad.net/pidgin-developers/ppa/ubuntu intrepid main
Read More…

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter