Skip to main content Link Search Menu Expand Document (external link)

How to run Ruby?

Table of contents

  1. Run your first ruby code (By file)
  2. Run your first ruby code (By irb)
  3. Explain to output something
  4. Operators
  5. 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.

  1. Create a new file.

     $~ vi hello_ruby.rb
    
  2. It will open the text editor page.

    ~
    ...
    ...
    ~
    "hello_ruby.rb" [New]
    
  3. Press a to change into insert mode.

    ~
    ...
    ...
    ~
    -- INSERT --
    
  4. Key in print("Hello Ruby.\n").

    print("Hello Ruby.\n")
    ...
    ...
    -- INSERT --
    
  5. Press ESC.(– INSERT –).

    print("Hello Ruby.\n")
    ...
    ...
       
    
  6. Key in :wq and press enter to save and quit vi.

    print("Hello Ruby.\n")
    ...
    ...
    :wq
    
  7. 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.

  1. Run irb.

     $~ irb
    irb(main):001:0>
    
  2. Key in print("Hello Ruby.\n") and enter.

    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

  1. print is method, "Hello Ruby.\n" is parameter. print means printing something (from parameter).
  2. What’s \n? It’s for newline.

    irb(main):001:0> print("Hello\nRuby.\n")
    Hello
    Ruby.
    => nil
    irb(main):002:0>
    
  3. Method puts. It will automatically add \n for each output.

    irb(main):001:0> puts("Hello", "Ruby")
    Hello
    Ruby
    => nil
    irb(main):002:0>
    
  4. 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>
    
  5. 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

  1. 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
    
  2. 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 value

This code assigns 1 into variable a.

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

Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.