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 to implement it.
A value can be returned by forcing a declaration of return.
The basis of the output method is to use puts (output line), print (direct printing).
The underlying input method is gets (read in a row, containing ' \ n ' at the end of the line, which can be eliminated by the Chomp method).
Pure phenomenon Object language, but also a dynamic language (although today is not yet available), so be honest object-oriented.
There are two simple ways to store multiple elements. Array [] and hash Table {}, and there are a variety of functions.
Highly recommend beginners online Learning Web site: http://tryruby.org/, it's a blast!
Various code
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 from file read data
# 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-
def load_stu (path)
data = {}
File.foreach (path) do |line|
Na, no = Line.split (')
s = student.new (No, NA)
data[s] = 1 end of
data End
def print_stu (da TA)
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 one line to calculate the gcd of these four numbers
# Enter 4 integers in one line, calculate these four number of GCD
def gcd (A, b)
if b = = 0 return
a
else return gcd (b, a% b)
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}"