Ruby learning note 1

Time:2020-2-11

Notes:

Ruby returns the last value in the method by default.
If you need to return more than one value, you can use an array.
You can return a value by forcing a return declaration.
The basic output method is to use puts (output one line), print (direct printing).
The basic input method is gets (read in a line, containing ‘\ n’ at the end of the line, which can be eliminated by chomp method).
It’s a pure phenomenal object language, and it’s also a dynamic language (although it’s not used today), so it’s really object-oriented.
There are two simple and crude ways to store multiple elements. Array [] and hash table {}, as well as various functions.
It is highly recommended that novice online learning website: http://tryruby.org/, which is so cool!
Various codes

EG1: create a hash table


ojs = {}
ojs["poj"] = :A
ojs["hdu"] = :A
ojs["uva"] = :B
ojs["zoj"] = :B
ojs["CF"] = :S
ojs["TC"] = :S
ratings = Hash.new(0)
ojs.values.each { |rate| ratings[rate] += 1 }
 print ratings
 puts ""
 puts "==============================="
 print ojs
 puts ""
 puts "==============================="
 3.times {print "hey!"}
 puts ""
 puts "==============================="
 
 print ojs.length
 puts ""
 print ojs.keys
 puts ""
 print ojs.values
 puts ""
 ojs.keys.each { |name| print name; print " "; print ojs[name]; puts ""; }
 puts "==============================="
 print File.read("x.txt")
 puts "==============================="
 File.open("x.txt", "a") do |f|
   f << "HACKED!\n"
   end
 print File.read("x.txt")
 puts "==============================="
 print File.mtime("x.txt")
 puts ""
 print File.mtime("x.txt").hour
 puts ""
 puts "==============================="

EG2: create a hash table by reading data from a file

#Read a file's database and output
def load_oj( path )
 ojs = {}
  File.foreach(path) do |line|
  name, value = line.split(':')
  ojs[name] = value
 end
 print_oj(ojs)
end
def print_oj( data )
 puts "================================"
 print "name\tvalue\n"
 data.keys.each do |name|
  puts "#{name}\t#{data[name]}"
 end
 puts "================================"
end
oj = load_oj("x.txt")

Eg3: read student information from file and output

#Read student information from file and output
class Student
#attr_accessor :name
#attr_accessor :number
def initialize(name = "Unknown", number = "2012309999")
@name = name
@number = number
end
def print
puts "#{@name}\t#{@number}"
end
end
def load_stu( path )
data = {}
File.foreach(path) do |line|
na, no = line.split(' ')
s = Student.new(no, na)
data[s] = 1
end
data
end
def print_stu( data )
puts "================================"
print "name\tnumber\n"
data.keys.each do |stu|
stu.print
end
puts "================================"
end
data = load_stu("y.txt")
print_stu(data)

EG4: enter 4 integers in a row to calculate the greatest common divisor of these four numbers

#Enter four integers in a row to calculate the greatest common divisor of these four numbers
def gcd(a, b)
 if b === 0
  return a
 else return gcd(b, a % b)
 end
end
str = gets.chomp
a, b, c, d = str.split(" ")
g1 = gcd(a.to_i, b.to_i);
g2 = gcd(c.to_i, d.to_i);
g3 = gcd(g1, g2)
puts "gcd(#{a}, #{b}, #{c}, #{d}) = #{g3}"