Wednesday, April 17, 2013

Installing Rails on Windows

This post intends to provide an insight into installing Rails framework on a Windows PC. Dont bother about the OS version. For now, just try and follow the steps mentioned. For users who are not able to install it as mentioned below, try the "google-out-the-error" alogirthm. 

The first tool to download will be the editor. An editor is an application assisting us to write programs. 
Sublime text will be your editor for Rails apps for a long time. Install Sublime text from here  We would write programs in this editor and use the command line tools to run and debug the programs.

The next tool to download will be the browser. A browser is an application that runs the Rails apps. 
Although it is recommended to use all the browsers to test Rails apps, you may just use Chrome for the moment. Download chrome from here 

The next tool to download will be the entire Ruby as well as Rails kit. The following packages come included in this kit, aptly called Ruby-Installer kit:
  1. Ruby 1.9.3-p392
  2. Rails 3.2
  3. Bundler
  4. Git 1.8.1.2
  5. Sqlite
  6. TinyTDS
  7. SQL Server support
  8. DevKit 
Download the ruby-installer from here . After the installation finishes, allow the command prompt to popup with the configurations for Git. 
There can be issues here - for e.g. if you are using a PC with Git already installed, then you may have to change the username and email. Also you may have to copy the public key to your git account(if you dont have a git account, then create one here) Additionally you can learn about Git here, if you wish to.
  1. set username : git config --global user.name "Dimple Gusain"  
  2. set email: git config --global user.email "dimple.supi@gmail.com"
  3. Navigate to C:\Users\user\.ssh and open the id_rsa.pub file in notepad
  4. copy(Ctrl+A) all the contents
  5. Login to your account on Github
  6. Navigate to the Account settings page(access this menu from top right on the home page)
  7. click on SSH keys menu on the left menu
  8. click Add Key on the middle right
  9. paste the key and don't forget to give a name like "Dimple Gusain PC"

As for continuing the installation process, Read through the manual here and it should be fairly easy. The document instructs you to create a rails project titled "railsinstaller_demo" and lets you run it from the localhost. If you are able to see the "Welcome aboard" page, you are done installing rails on your machine. 

So far we have everything in place to start a rails project, and in fact the tools you just installed also helped you create a project. But we still need to edit the program in the editor.

Setting up Sublime Text editor:
  1. navigate to C:\Program Files (x86)\Sublime Text 2 (or C:\Program Files\Sublime Text 2 on some machines)
  2. locate the Sublime_Text_2.exe . Copy and paste it again in the same location. You should be able to see a file called Sublime_Text_2_copy.exe 
  3. Rename it to subl.exe
  4. Right-click on My computer and click Properties. 
  5. Click Advanced System Settings and click Advanced Tab. Click Environment variables button.
  6. In the "user variables for user" section, click edit.
  7. Add a semi-colon and the path of the exe file earlier. for e.g. blah;blah;C:\Program Files (x86)\Sublime Text 2 
  8. click ok  
Now close the command prompt and open it again(this restarts the command prompt and adds any new paths added). Type the following commands:
  1. type "cd.." as required to return to the root folder or the C:
  2. cd sites
  3. cd railsinstaller_demo
  4. subl . (this command invokes the sublime text editor with the contents of the current directory) This might take a few seconds to load depending on how fast your system is. Patience and peace.
  5. Now that you can see the project in the editor, you would want to the see the editor in action by editing something.
  6. Click on public folder and right click on the index.html to rename it to index_old.html
  7. Goto command prompt and create a new controller by typing "rails g controller welcome index"(this would create a couple of files, regarding which you neednt worry much now)
  8. Come back to the editor and on the left section(called navigator) locate the config/routes.erb
  9. uncomment the line root :to => 'welcome#index' and save the file(Ctrl+s)
  10. Also navigate to app/views/welcome/index.html.erb and edit the content between the <h1> tag to say "Dimple Gusain Rocks!". Save the file
  11. Go back to command prompt and type rails s
  12. Open the browser and type http://localhost:3000 to see the new edits
 What we just did:
  1. In Rails, every view has an associated controller. 
  2. So instead of creating a view(or a .html page), we create the controller(rails g controller welcome index) and Rails creates the view plus a couple of other files for us.
  3. Upon receiving a URL request, the default behavior of the rails framework is to look in the public folder for an image or file by that name. If not found, it will look in the routes.erb to locate the file. 
  4. We edited both the files to make sure the newly created welcome page is loaded when the root location is accessed. 
  5. We then started the rails server and accessed the application root to see the change


Committing the changes to Git and pushing the commits to Github
  1. in order to record the commits, you will need to install git in the current directory. git init
  2. add all the files to git git add .
  3. commit the changes git commit -m "Dimple Gusain's first commit"
  4. create a new repository in your account to hold a copy of the application online.
  5. log into your account on github. Create a new repository at the bottom right.
  6. name it "Dimple Supi" or just about any name that you like. Let the read me file be there too
  7. we will need to frequently push changes to the online copy of a project so we store the path in a variable for easy access.  git remote add origin git@github.com:supimon/Dimple Supi.git
  8. we then use this variable (origin) to push the changes to the online copy git push origin master
  9. in case of conflicts, type git push -f origin master to force update the changes(remember: with force update, all the changes on the online copy will be lost but since we are just starting this should be fine) 

A retrospective view:
  1. We set up the Rails programming environment by installing all the necessary tools.
  2. We set up an online repository to store a copy of the project online.
  3. We created and edited a few files.
  4. We initiated git in the project and committed the changes.
  5. We then pushed the commits online.
  6. Kool :)

That's all folks!

  

Wednesday, December 12, 2012

A quick look at the basics

Ruby. Real programming. Real deal.


Its far too early to be judging anything about this language but looking at its popularity among the core hackers of this planet, there really is no doubt that it's an awesome language. Because those hackers are not sitting idle, but are contributing to Ruby in a major way. 
Check out code school for e.g. The guys there have done an awesome job and continue to do so. In fact I am here to share what I have learned from there and would recommend it to all those who read. Buy their courses, its worth more than what you spend. They have these interactive medium for learning that helps finish courses pretty fast. I also loved the dark theme to their videos and teachings >=) 

Please read this blog simultaneously or after you have completed the course at code school.

      - yellow indicates important texts. 
      - yellow brown indicates codes.


Meet Ruby Objects


In Ruby, Numbers and Strings are Math and Text objects. They have english and symbolic methods like "reverse" and "*".
E.g. "Dimple".reverse"Gusain"*5 etc

The 'lines' method on a string object decides how the string will split up. 
For e.g. 
poem = "Bang, Bang, He shot me down. Bang, Bang, I am going down. Bang, Bang that awful sound. Bang, Bang I am going down."
poem.lines.to_a.reverse (convert it to an array and reverse it)
print poem.lines.to_a.reverse.join (… convert it back to string and print it)
  1. This is a string - "Dimple"
  2. This is a symbol - :supi
Symbols are cheaper than strings in terms of computer memory. When there is a string that occurs over and over in the program, go for a symbol. Its very easy to convert a string to symbol. Just precede it with a colon. Unlike a string, computers store the symbols only once.


Ruby Terminologies: 


  1. "lists" -  are similar to arrays. Example of a list: dimple = []
  2. "hashes" -  are similar to objects or dictionaries. The length method works on both of them. Example of a hash: supi = {}
books["<key>"] = :<value> (this is if the value is a symbol)
For e.g. books["In Love with Dimple Gusain"] = :supi , where In Love with Dimple Gusain is the key and :supi is the value
another way to instantiate an empty hash is -  dimple= Hash.new(0)


Digging into Ruby:


books.values.each { |rate| ratings[rate] += 1}
Simplification of the above code:
  • books is a hash with many values.
  • Each time, for the total length of the items in books, take each value and pipe it as an argument into the variable 'rate'. 
  • Create a new item in ratings hash with the key 'rate' and value 1 OR 
  • Increment the rating if its already there ,by 1 for each entry of the variable.
  • ratings = {} is implied

The bottom line is: **that while some methods take variables as arguments, some others take functions or blocks as arguments.** 
In the above example, the method 'each' takes the block indicated by the curly braces as the arguments and executes it in a for loop that equals the length of the hash. The beauty of this method is that it also provides each value of the hash as argument to the block for each iteration.
5.times {print "Dimple Supi"} is another example


Files and Directories in Ruby


  • Dir method holds the directories relative to the ruby executable(I guess?)
  • entires method lists all the directories and files for a supplied path. For e.g. - - Dir.entries "/"
  • read contents of a file: File.read("/Home/comics.txt") 
  • write to a file by appending: 
  • File.open("/Home/comics.txt", "a") do |f| 
  • f << "supi and dimple: http://supianddimple.com/"
  • end
  • Copy file contents to the specified path, if no such file then create one:FileUtils.cp(/comics.txt', 'Home/comics.txt')
  • modified time for a file: File.mtime("/Home/comics.txt")
  • 'foreach' method on file returns the contents of the file in a line for the total number of lines in the file.


Method definition in Ruby:


  • starts with 'def' keyword and ends with 'end' keyword. 
  • For e.g. 
def load_comics(path) 
comics = {}
File.foreach(path) do |line|
name, url = line.split(": ") // splits the string to array and assigns the first element to first variable, second to second, etc
comics[name] = url.strip // strips any associated whitespace 
end
comics
end

Use the 'require' method to load libraries. for e.g. require "dimplegusain"


A class in Ruby:


  • class DimpleGusain
  • You can add attributes to the class as follows: attr_accessor
  • You can also add methods later with the def statements. the initialize method is called every time the new keyword is used to instantiate an object from the class
  • We always use @<attribute> (without the colons of course) for accessing the attribute within the class but we always use the accessor method outside the class. supi.time, supi.title etc
  • by the by, supi = DimpleGusain.new was implied


Methods on an array in Ruby:


  • 'sort_by' method on an array takes in arguments as blocks so that it can sort the array based on that.
  • 'map' method replaces the array elements with whatever the block returns for each iteration . For e.g. supi.map {"Dimple Gusain} Every entry in supi is now replaced with Dimple Gusain 


More posts on beginner perspective follows soon after I am done with rails ;) 
\,,/