Ruby Language


This draft deletes the entire topic.

expand all collapse all

Examples

  • 13

    This example assumes Ruby is installed.

    Place the following in a file named hello.rb:

    puts 'Hello World'
    

    From the command line, type the following command to execute the Ruby code from the source file:

    $ ruby hello.rb
    

    This should output:

    Hello World
    

    The output will be immediately displayed to the console. Ruby source files don't need to be compiled before being executed. The Ruby interpreter compiles and executes the Ruby file at runtime.

  • 11

    You can add an interpreter directive (shebang) to your script. Create a file called hello_world.rb and write the following script:

    #!/usr/bin/env ruby
    # file: hello_world.rb
    
    puts 'Hello World!'
    

    Change the script executable permissions. Now, you do not need to call the Ruby interpreter directly from the command line when you run your script.

    $ chmod u+x hello_world.rb
    $ ./hello_world.rb
    
  • 7

    Alternatively, you can use the Interactive Ruby Shell (IRB) to immediately execute the Ruby statements you previously wrote in the Ruby file.

    Start an IRB session by typing:

    $ irb
    

    Then enter the following command:

    puts "Hello World"
    

    This results in the following console output (including newline):

    Hello World
    

    If you don't want to start a new line, you can use print:

    print "Hello World"
    
  • 4

    Run the command below in a shell after installing Ruby. This shows how you can execute simple Ruby programs without creating a Ruby file:

    ruby -e 'puts "Hello World"'
    

    You can also feed a Ruby program to the interpreter's standard input. One way to do that is to use a here document in your shell command:

    ruby <<END
    puts "Hello World"
    END
    
Please consider making a request to improve this example.

Remarks

Ruby is a multi-platform open-source, dynamic object-oriented interpreted language, designed to be simplistic and productive. It was created by Yukihiro Matsumoto (Matz) in 1995.

According to its creator, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp. It supports multiple programming paradigms, including functional, object-oriented, and imperative. It also has a dynamic type system and automatic memory management.

Versions

VersionRelease Date
2.32015-12-25
2.22014-12-25
2.12013-12-25
2.02013-02-24
1.92007-12-25
1.82003-08-04
1.6.82002-12-24
Still have a question about Getting started with Ruby Language? Ask Question

Topic Outline