Advertise

Monday, 16 September 2013

Programming with Ruby in Ruby Part 1 [ Introduction - Hello World Tutorial ]

What is Ruby Programming ?

Ruby is an interpreted, object-oriented programming language. Its creator, Yukihiro Matsumoto, a.k.a “Matz,” released it to the public in 1995. Its history is covered here. Its many features are listed here.

How to Get Ruby and Install Ruby in our System ?



Ruby comes preinstalled on Mac OS X and many Linux distributions. In addition, it is available for most other operating systems, including Microsoft Windows.

To find the easiest way to install Ruby for your system, follow the directions below. You can also install Ruby by compiling the source code, which can be downloaded from the Ruby web site.


Ruby comes preinstalled on Mac OS X. To check what version is on your system:
  1. Launch the Terminal application, which is located in the "Utilities" folder, under "Applications".
  2. At the command-line, enter: ruby -v

If you want to install a more recent version of Ruby, you can:
Buy the latest version of Mac OS X, which may have a more recent version of Ruby.
Install Ruby using RVM. (This is the most popular way because you can manage ruby versions and install many other ruby packages)
Install Ruby using Fink.
Install Ruby using MacPorts.
Install Ruby using Homebrew.

In Windows


Ruby does not come preinstalled with any version of Microsoft Windows. However, there are several ways to install Ruby on Windows.
  1. Download and install one of the compiled Ruby binaries from the Ruby web site.
  2. Download and run the one click RubyInstaller.
  3. Install Cygwin, a collection of free software tools available for Windows. During the install, make sure that you select the "ruby" package, located in the "Devel, Interpreters" category.


For More Query About Hot to Install, I would recommend you to visit :- 
For Windows Google Search Results for: How to Install Ruby in Windows.
For Linux :- Google Search Results for: How to Install Ruby in Linux.
For Macintosh :- Google Search Results for: How to Install Ruby in Macintosh.

So, I assuming you Done with Ruby Installation, if any issue, ask this in forum, i continue to help you there 

First Program in Ruby - Ruby Hello World


1. Open Notepad code down below :-

PHP Code:
puts 'Hello world' 

2. Save this File "HelloWorld.rb" and run your command prompt :-

PHP Code:
c:\>ruby HelloWorld.rb
Hello world 

And you see Hello world shouting out in command prompt  congratulation, you coded first program in Ruby.

You can also run the short "hello world" program without creating a text file at all. This is called a one-liner.
PHP Code:
ruby -"puts 'Hello world'"Hello world 
You can run this code with irb, but the output will look slightly different. puts will print out "Hello world", but irb will also print out the return value of puts – which is nil.
PHP Code:
irb>> puts "Hello world"Hello world=> nil 

Like Perl, Bash, and C Shell, Ruby uses the hash symbol (also called Pound Sign, number sign, Square, or octothorpe) for comments. Everything from the hash to the end of the line is ignored when the program is run by Ruby. For example, here's our hello-world.rb program with comments.
PHP Code:
# My first Ruby program
# On my way to Ruby fame & fortune!
 
puts 'Hello world' 
You can append a comment to the end of a line of code, as well. Everything before the hash is treated as normal Ruby code.
PHP Code:
puts 'Hello world'                # Print out "Hello world" 
You can also comment several lines at a time:
PHP Code:
=begin
This program will
print "Hello world".
=
end

puts 
'Hello world' 
Although block comments can start on the same line as =begin, the =end must have its own line. You cannot insert block comments in the middle of a line of code as you can in C, C++, and Java, although you can have non-comment code on the same line as the =end.
PHP Code:
=begin This program will print "Hello world"=end puts 'Hello world' 

Unix-like operating systems

Typing the word ruby each time you run a Ruby script is tedious. To avoid doing this, follow the instructions below.
In Unix-like operating systems – such as Linux, Mac OS X, and Solaris – you will want to mark your Ruby scripts as executable using the chmod command. This also works with the Cygwin version of Ruby.
PHP Code:
chmod +x hello-world.rb 
You need to do this each time you create a new Ruby script. If you rename a Ruby script, or edit an existing script, you do not need to run "chmod +x" again.
Next, add a shebang line as the very first line of your Ruby script. The shebang line is read by the shell to determine what program to use to run the script. This line cannot be preceded by any blank lines or any leading spaces. The new hello-world.rb program – with the shebang line – looks like this:
PHP Code:
#!/usr/bin/ruby
puts 'Hello world' 

If your ruby executable is not in the /usr/bin directory, change the shebang line to point to the correct path. The other common place to find the ruby executable is/usr/local/bin/ruby.

The shebang line is ignored by Ruby – since the line begins with a hash, Ruby treats the line as a comment. Hence, you can still run the Ruby script on operating systems such as Windows whose shell does not support shebang lines.

Now, you can run your Ruby script without typing in the word ruby. However, for security reasons, Unix-like operating systems do not search the current directory for executables unless it happens to be listed in your PATH environment variable. So you need to do one of the following:
1. Create your Ruby scripts in a directory that is already in your PATH.
2. Add the current directory to your PATH (not recommended).
3. Specify the directory of your script each time you run it.

Most people start with #3. Running an executable Ruby script that is located in the current directory looks like this:

PHP Code:
$ ./hello-world.rb 

Once you have completed a script, it's common to create a ~/bin directory, add this to your PATH, and move your completed script here for running on a day-to-day basis. Then, you can run your script like this:

PHP Code:
hello-world.rb 

If you do not want to hard-code the path to the ruby executable, you can use the env command in the shebang line to search for the ruby executable in your PATH and execute it. This way, you will not need to change the shebang line on all of your Ruby scripts if you move them to a computer with Ruby installed in a different directory.

Using Env

PHP Code:
#!/usr/bin/env ruby
puts 'Hello world' 

Windows

If you install the native Windows version of Ruby using the Ruby One-Click Installer, then the installer has setup Windows to automatically recognize your Ruby scripts as executables. Just type the name of the script to run it.

PHP Code:
hello-world.rb
Hello world 

If this does not work, or if you installed Ruby in some other way, follow these steps.
Log in as an administrator.
Run the standard Windows "Command Prompt", cmd.
At the command prompt (i.e. shell prompt), run the following Windows commands. When you run ftype, change the command-line arguments to correctly point to where you installed the ruby.exe executable on your computer.

PHP Code:
assoc .rb=RubyScript.rb=RubyScript
ftype RubyScript="c:\ruby\bin\ruby.exe" "%1" %*RubyScript="c:\ruby\bin\ruby.exe" "%1" %* 
 
World of Hacker © 2011 Creative Commons License
World of Hacker by KroKite is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at http://www.worldofhacker.com.
Permissions beyond the scope of this license may be available at https://groups.google.com/forum/#!newtopic/hackerforum.