Friday, November 30, 2012

Getting Started with Ruby

After more than two years of absence from my blog I thought of coming back to document something with which I've got my hands on these days. After coding for about an year in python I thought of giving Ruby on rails a go. But one thing I discovered when I went to setup ruby was that there wasn't any consistent guide that explained how to setup ruby on both linux & mac environments. So I thought of documenting what I went through when I first setup Ruby on my mac & linux machines with the hope that this would become useful to someone else in the future.
            First of all if you're planning to start doing web development with a scripting language like ruby or Python it's always a best practice to setup your development environment in either a linux box or a mac. From past experience I could say that Setting up Ruby/Python on a windows box & getting it up and running for software development is simply just painful.
             To get things going firstly we need to install the RVM or the Ruby Version Manager. This is something similar to the Python Virtualenv utility that enables us to install several versions of ruby on the same machine.Plus it's also the way how 'cool kids' install Ruby these days.
  •  To install the RVM first download using curl on your Linux box or your mac using the terminal.  
 curl -L get.rvm.io | bash -s stable  
       
           If you're using a linux box (in my case ubuntu 12.04 LTS) you also can simply install rvm using the apt-get utility.

 sudo apt-get install ruby-rvm  
  • Then On mac you have to make the RVM available in the terminal by editing the .bash_profile     file as follows. In linux this is usually added automatically.
 cd ~/  
 sudo vim .bash_profile  

          Then add the following line at the bottom of that file as follows,then quit and reopen the terminal again.

 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"  
  •  If you're on a mac before proceeding further to the next step make sure you have the XCode  latest version installed on your system with the command line tools support.
  • Now you can check the available  versions of ruby using the rvm tool.
 rvm list known  

  • Now you could simply install the latest version of ruby using the rvm as shown below. The latest version number of Ruby at the time of writing is 1.9.3. Make sure you always install the latest version.
 rvm install 1.9.3  

  • Now make sure you're making use of the latest installed version of the ruby.
 rvm use 1.9.3  

      If you get an error when running the above command on a linux terminal then  select preferences
      from the edit menu and make sure you've checked the "run command as login shell" check box 
     under the "title and command" tab.
  • Now let's make the latest version of ruby the default.
 rvm --default use 1.9.3  

      You can also check the current version of the ruby like;

 ruby -v  

Important : When you're running the above commands on the terminal make sure you're not using the 'sudo' as it would simply install and configure ruby for the root user and not the current user.
  • Ok now we've installed ruby properly it's time to install rails. The rails come as a gem in ruby,which is similar to a package in python. For installing gems in Ruby we could use the 'gem' command,which is the 'pip' equivalent of python for Ruby.
 gem install rails  
  • Once you've installed rails on your machine it's time to test it out by creating a sample application. This is as simple as typing a single command in Ruby.
 rails new mynewappname  

The above command will generate all the files and folder structures of a sample web application inside a folder named "mynewappname",this will be more than enough for you to get started on developing your first web application out of the box.
  • Now you can simply run  the web application  using the Ruby's development server.
 cd mynewappname  
 rails server  

Note: If you get and error when you run the development server in Linux saying it couldn't find a Javascript runtime then make sure you've installed the Node.js as shown here.
  • Now you could view the sample/demo web application on your local web browser. By connecting to localhost on port 3000.
 localhost:3000/mynewappname  


Conclusion

This article shows how quick and easy it is to start developing dashing web applications using the Ruby on Rails. And these days in the ever changing and rapidly progressing world of web development , language tools like Ruby on Rails and Django python leads the pack !!. Hope this post was helpful to you. If you have any questions or doubts with regard to the Ruby on Rails setup please drop a comment below.

Thanks,
./Ramesh




Some Useful MySQL Commands Used Day to Day

Today I thought of presenting you with something of a little different taste.Therefore I thought of publishing some useful and more common mysql commands that will be useful to developers in their day to day development tasks. As mysql is the most commonly used opens ource database in java EE based development I think this post content will be useful to a lot of novice fresh graduate developers who are entering the industry. I've used a small mysql comment line to explain briefly the purpose of each command mentioned below.
  -- Getting a MySQL Dump(From remote DB):  
  mysqldump -u root -ppassword -h server_name db_name > database_dump_name.sql 
   
  -- Restore a MySQL Dump(To remote DB):    
  mysql -u root -ppassword -h server_name db_name < database_dump_name.sql 
   
  -- Output Data To a File    
  SELECT * FROM table_name INTO OUTFILE 'c:/file_name.file_extention';   
 
  -- Optimize Data Table : Used to defragment the table data file    
  optimize table table_name;    

  -- Monitor/View MySQL Memoty Usage On Linux /shell    
  top -u mysql    

  -- Create an Index for an existing Table In MySQL    
  CREATE INDEX table_column ON table_name(table_column);    

  -- Create a New User and Assign him all privileges to an existing database   
   GRANT ALL ON cpsdb.* TO user@localhost IDENTIFIED BY 'password';