How to run Ruby?
Table of contents
- Run your first ruby code (By file)
- Run your first ruby code (By irb)
- Explain to output something
- Operators
- Variable
First of all, you need to check your development environment with Ruby or not.
All macOS installed Ruby by default.
$~ ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]
Run your first ruby code (By file)
vi
is a screen-oriented text editor originally created for the Unix operating system. We will use vi to edit files in this textbook.
Create a new file.
$~ vi hello_ruby.rb
It will open the text editor page.
~ ... ... ~ "hello_ruby.rb" [New]
Press
a
to change into insert mode.~ ... ... ~ -- INSERT --
Key in
print("Hello Ruby.\n")
.print("Hello Ruby.\n") ... ... -- INSERT --
Press
ESC
.(– INSERT –).print("Hello Ruby.\n") ... ...
Key in
:wq
and pressenter
to save and quit vi.print("Hello Ruby.\n") ... ... :wq
Enter command
ruby + {filename}
. $~ ruby hello_ruby.rb Hello Ruby.
Run your first ruby code (By irb)
The Interactive Ruby Shell (irb) is a tool that enables you to interactively run Ruby programs and statements from the command line.
Run irb.
$~ irb irb(main):001:0>
Key in
print("Hello Ruby.\n")
andenter
.irb(main):001:0> print("Hello Ruby.\n") Hello Ruby. <- method print result => nil <- this is return value, we will talk in the furture irb(main):002:0>
Explain to output something
print
is method,"Hello Ruby.\n"
is parameter.print
means printing something (from parameter).What’s
\n
? It’s for newline.irb(main):001:0> print("Hello\nRuby.\n") Hello Ruby. => nil irb(main):002:0>
Method
puts
. It will automatically add\n
for each output.irb(main):001:0> puts("Hello", "Ruby") Hello Ruby => nil irb(main):002:0>
Method
p
, It will print include the parameter type, like String or Integer.irb(main):001:0> puts 100 100 => nil irb(main):002:0> puts "100" 100 => nil irb(main):003:0> p 100 100 => 100 irb(main):004:0> p "100" "100" => "100" irb(main):008:0>
Calling methods will still work without
()
.irb(main):001:0> print("Hello Ruby.\n") Hello Ruby. => nil irb(main):002:0> print "Hello Ruby.\n" Hello Ruby. => nil
Operators
Value
irb(main):001:0> 1 => 1 irb(main):002:0> 1.class => Integer -> this is 1 object created by Integer class, we will talk this in the future irb(main):003:0> 3.14 => 3.14 irb(main):004:0> 3.14.class => Float -> this is 3.14 object created by Float class, we will talk this in the future
Operators will follow the PEMDAS rule.
irb(main):001:0> 1 + 1 => 2 irb(main):002:0> 2 - 3 => -1 irb(main):003:0> 5 * 10 => 50 irb(main):004:0> 100 / 4 => 25 irb(main):005:0> 20 + 8 / 2 => 24 irb(main):006:0> (20 + 8) / 2 => 14
Variable
Area and Volume calculation, you can put variable in print
.
a = 1
a
is the variable
=
is use to assign values
1
is valueThis code assigns
1
into variablea
.
irb(main):001:0> x = 10
=> 10
irb(main):002:0> y = 20
=> 20
irb(main):003:0> z = 30
=> 30
irb(main):004:0> area = (x*y + y*z+ z*x)
=> 1100
irb(main):005:0> volumn = x * y * z
=> 6000
irb(main):006:0> print "area=", area, "\n", "volumn=", volumn,"\n"
area=1100
volumn=6000
=> nil