Skip to content

Ruby

History and fundamentals

  1. Ruby

  2. Programming paradigm: Ruby can do all (need further explanation)

    • Procedural: data and functions not associated
    • Functional: anonymous functions
    • Object oriented: data and algorithms are associated
      • class based: instantiate
      • prototype based: clone

Installation

Method 1: Docker

Download THIS Dockerfile and run the following script. [tag] is the tag name for image, whose format is name:version, eg. ruby:2.5.1

1
2
3
4
```sh
docker build -f ruby.dockerfile -t [tag] .
docker run -it tag
```

Method 2: installation in the system

Reference: official Ruby website

Environment

  1. irb: interactive Ruby
  2. .rb: Ruby program

Basics

Glance of Ruby

  1. Structure
    • Methods definitions
    • Statements that call methods
    • Classes with methods
    • Statements that create objects
    • Other statements

??? example "MyClass.rb"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
``` ruby tab="Ruby"
class MyClass           # define a class
    def initialize(var) # constructor method
        @var=var
    end

    def my_method       # instance method
        p "instance var is #{@var}"
    end
end

obj = MyClass.new(3)
obj.my_method
```

Data Structure

Everything is a object

Numeric

  1. Integer

    • 1.even?, 1.odd?, 1.zero?, 1.abs: ? belongs to methods names.
    • 23456.chr(Encoding::UTF_8) default encoding in ASCII
    • .next, .succ same.
    • to_s(system) to string, default in decimal system
    • Iterate methods
      • 5.times {|x| p x}: transfer 0-4 into block as x
      • 5.upto(10) {|x| p x}: transfer 5-10 into block as x
      • 5.downto(1) {|x| p x}: transfer 5-1 into block as x
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    # Decimal
    123, 0d123, 408_345_6789
    
    # binary
    0b100111
    
    # octal
    0123m 0o123
    
    # hex
    0x7B
    
  2. Float

    1
    2
    3
    # Decimal
    0.123e2     # 12.3
    123e-1      # 12.3
    
  3. Rational

  4. Complex
  5. Get class

    1
    2
    3
    # Decimal
    p "".class
    p 1.class
    

Range

  1. Format:

    • (1..5) or Range.new(1, 5, false): include the end value
    • (1...5) or Range.new(1, 5, true) not include the end value
  2. Method

    • (1..5).end == (1...5).end, (1..5).begin == (1...5).begin: although the later not include the end point.
    • (1...5).exclude_end? => true
    • .cover?(e) or .include?(e), return true if e INCLUDED IN RANGE (no need to be explicit)
      • (1..5).cover?(2.5) => true
      • (car’..’cat).include?(cas) => true
    • .first(n), .last(n)
    • Element number
      • .count
      • .size not work for non integer sequence.
    • (..).step(n): same as range(start, end, step) in Python.
    • .min, .max
  3. Iterative: provides each element to iterative blocks

    1
    (1..10).each {|x| p x, '-'}
    

    or

    1
    2
    3
    (1..10).each do |x|
        p x, '-'
    end
    

    is the same to

    1
    2
    3
    for x in (1..10) do
        p x, '-'
    end
    

String

  1. Format:

    • Quote: only double quoted string will be parsed
    • %
      • Followed by Q{} or one in {}, ( ), < >, [ ], / /, * *, # #, ! !, @@, &&, ^^, ++, - -,| |, : :, ; ; % % will be parse.
      • Followed by a pair of q{} won't be parsed.
      • Convenient when contents includes quote marks.
    1
    2
    3
    4
    5
    6
    7
    str1 = 'a string \n'        # => "1 + 1 is \#{1+1}"
    str2 = '1 + 1 is #{1+1}'    # => "1 + 1 is \#{1+1}"
    str2 = %q{1 + 1 is #{1+1}}  # => "1 + 1 is \#{1+1}"
    str3 = "a string \n"        # => "1 + 1 is 2"
    str4 = '1 + 1 is #{1+1}'    # => "1 + 1 is 2"
    str2 = %Q{1 + 1 is #{1+1}}  # => "1 + 1 is 2"
    str5 = String.new("another string")
    
  2. Here document (show as it is)

    1
    2
    3
    4
    x = <<END_MARK
    Contents
        Contents
    END_MARK
    

    ??? note "puts vs. p"

  3. Methods

    • Concatenation

      • <<: same as +.
      1
      2
      3
      4
      5
      "i love you " + "forever"       # "i love you forever"
      "i love you ""forever"          # "i love you forever"
      "over " * 3 + " again"          # "over over over again"
      a = "i love you"
      a << "r money"                  # "i love your money"
      
    • <=> operator: returns -1, 0, or 1 for <, =, and > correspondingly.

      1
      2
      3
      "1" < "2"       # -1
      "1" < "1"       # 0
      "2" < "1"       # 1
      
    • Slicing

      1
      2
      3
      4
      5
      6
      a = "0123456"
      a[0]            # "0"
      a[2, 5]         # start from 2, 5 elements
      a[2..5]         # "2345"
      a[2...5]        # "234"
      a.length        # 7
      
  4. Iterative: provides each character to iterative blocks

    1
    "iloveyou".each_char {|x| print x, '-'}     # i-l-o-v-e-y-o-u-
    

Symbol

  1. Hash:
1
2
person[:name]  = 'yuan'
person['name'] = 'yuan'     # NOT same as above
  1. Method arg
1
2
3
4
5
6
7
8
9
class Test
    def tmp
        puts "symbol method"
    end
end

myObj = Test.new()
m = :tmp
obj.method(m).call      # => "symbol method"

Containers

Array

  1. Similar to Python list, can have diff types of element.

Wired

  1. Week 1-2 slide 11 a[2, 5]
  2. Week 1-2 slide 22 [:name] and ['name']

TBD

  • include: add everything belongs to the included class/module (class var, inst method, module const)
  • extend: add inst method as class method (extend it to single subclass)

  1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.