Tuesday, June 25, 2013

Hello World Ruby Program

Every programming language starts with Hello World Program.
Here is Hello World Ruby Program.

Create a text file called hello-world.rb containing the following code:
puts 'Hello world'
Now run it at the shell prompt.
$ ruby hello-world.rb
Hello world
You can also run the short "hello world" program without creating a text file at all. This is called a one-liner.
$ ruby -e "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.
$ irb
>> puts "Hello world"
Hello world
=> nil

 object oriented version of Hello World Program
class HelloWorld
   def initialize(name)
      @name = name.capitalize
   end
   def sayHi
      puts "Hello #{@name}!"
   end
end

hello = HelloWorld.new("World")
hello.sayHi
execute online

No comments:

Post a Comment