Contents
You may also like ...................................................................................................................................................... 226
About
Please feel free to share this PDF with anyone for free, latest version of this book can be downloaded from: https://goalkicker.com/RubyBook
This Ruby Notes for Professionals book is compiled from Stack Overflow Documentation , the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not affiliated with official Ruby group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective
company owners
The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk
Please send feedback and corrections to web@petercv.com
Chapter 1: Getting started with Ruby Language
Version Release Date
2.5.1 2018-03-28
2.4 2016-12-25
2.3 2015-12-25
2.2 2014-12-25
2.1 2013-12-25
2.0 2013-02-24
1.9 2007-12-25
1.8 2003-08-04
1.6.8 2002-12-24
Section 1.1: Hello World
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.
Section 1.2: Hello World as a Self-Executable Fileusing Shebang (Unix-like operating systems only)
You can add an interpreter directive (shebang) to your script. Create a file called hello_world.rb which contains:
#!/usr/bin/env rubyputs '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 Ruby interpreter explicitly to run your script. $ .
/ hello_world.rb
Section 1.3: Hello World from IRB
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"
Section 1.4: Hello World without source files
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
Section 1.5: Hello World with tk
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.
TkRoot.new
{ title "Hello World!"
} Define a widget with the title Hello World
Tk.mainloop
Start the main loop and display the widget.
Section 1.6: My First Method
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 output should be:
Hello world!
Explanationdef is a keyword that tells us that we're def -ining a method - in this case, hello_world is the name of our method.
puts "Hello world!" puts (or pipes to the console) the string Hello world!
end is a keyword that signifies we're ending our definition of the hello_world method as the hello_world method doesn't accept any arguments, you can omit the parenthesis by invoking the method
Chapter 2: Casting (type conversion)
Section 2.1: Casting to a Float
"123.50" .to_f
#=> 123.5Float ( "123.50"
)#=> 123.5 However, there is a difference when the string is not a valid
Float :
"something" .to_f
#=> 0.0Float ( "something"
)# ArgumentError: invalid value for Float(): "something"Section 2.2: Casting to a String
123.5 .to_s
#=> "123.5"String ( 123.5
)#=> "123.5" Usually,
String () will just call
#to_s .
Methods
Kernel#sprintf and
String#% behave similar to C:
sprintf ( "%s" , 123.5 )#=> "123.5"
"%s" % 123.5 #=> "123.5"
"%d" % 123.5 #=> "123"
"%.2f" % 123.5 #=> "123.50"
Section 2.3: Casting to an Integer
"123.50" .to_i
#=> 123Integer ( "123.50"
)#=> 123 A string will take the value of any integer at its start, but will not take integers from anywhere else:
"123-foo" .to_i
# => 123 "foo-123" .to_i
# => 0 However, there is a difference when the string is not a valid Integer:
"something" .to_i
#=> 0Integer ( "something"
)# ArgumentError: invalid value for Integer(): "something"Section 2.4: Floats and Integers