Tutorial by Examples

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 (in...
Tk is the standard graphical user interface (GUI) for Ruby. It provides a cross-platform GUI for Ruby programs. Example code: require "tk" TkRoot.new{ title "Hello World!" } Tk.mainloop The result: Step by Step explanation: require "tk" Load the tk package...
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 di...
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...
You can add an interpreter directive (shebang) to your script. Create a file called hello_world.rb which contains: #!/usr/bin/env ruby puts 'Hello World!' Give the script executable permissions. Here's how to do that in Unix: $ chmod u+x hello_world.rb Now you do not need to call the Ru...
Overview Create a new file named my_first_method.rb Place the following code inside the file: def hello_world puts "Hello world!" end hello_world() # or just 'hello_world' (without parenthesis) Now, from a command line, execute the following: ruby my_first_method.rb The ou...

Page 1 of 1