As of the past few weeks, I am completely new to the web programming world of Ruby and was definitely apprehensive prior to starting my course in web technologies, however having said that, Ruby does indeed seem to be quite approachable in terms of being able to understand it and to learn it relatively easily.
To install Ruby, simply click here to download for free. Once installed, it can be simply run by opening the command prompt (Click 'run' then type in 'cmd' and hit enter. Alternatively, you can try Ruby in a web browser by clicking here in a series of progressively harder sample tutorials. This is a great way of seeing exactly what it is if you are new to the concept of programming or the ruby application.
The most simple piece of code is the 'hello world' application. Once the command prompt is open, type in irb and hit enter. This is an interactive command-line interpreter and allows us to test ruby directly in the command prompt, rather than saving our code as a .rb file and then running it from the command prompt. So enter irb, and then type in puts "hello world" and hit enter. The command prompt will then return the following:
Hello, World
=> nil
So that is a basic program believe it or not! Here is a basic piece of code from the official Ruby site:
# This is an ignored comment
cities = %w[ London
Oslo
Paris
Amsterdam
Berlin ]
visited = %w[Berlin Oslo]
"to visit the " +
"following cities:",
cities - visited
If you copy and paste the above text into the command prompt, it will return the following:
I still need to visit the following cities: LondonParisAmsterdam
=> nil Here is my explanation of the code:
- The hash (#) key tells ruby that the text that follows it on that line is like a comment, ie. it is not part of the code so ruby will ignore it.
- The "cities =" is just creating a group of cities.
- The "visited=" is creating a group from the cities listed that we have visited.
- Then the "puts" command is telling ruby to return the text in quotes, but to return the 'cities - visited' i.e. to return the cities listed but not the ones we have visited, hence why the visited cities are missing from the output!
That is a very basic introduction into the ruby programming language!