We strongly recommend that you use the following Online Learning Websites: http://tryruby.org:
- Ruby returns the last value in the method by default.
- To return multiple values, you can use an array.
- You can return a value by forcibly declaring return.
- The basic output method is to use puts (output line) and print (print directly ).
- The Basic Input Method is gets (read a row that contains '\ n' at the end of the row, which can be eliminated by chomp ).
- The pure phenomenon object language is also a dynamic language (although it is not used today), so it is honestly object-oriented.
- There are two simple and crude methods to store multiple elements. Array [] and hash table {}, and there are various functions.
- We strongly recommend that you use the beginner's Online Learning Website http://tryruby.org!
Various code eg1: Create a hash table
Ruby code
1 ojs = {} 2 ojs["poj"] = :A 3 ojs["hdu"] = :A 4 ojs["uva"] = :B 5 ojs["zoj"] = :B 6 ojs["CF"] = :S 7 ojs["TC"] = :S 8 ratings = Hash.new(0) 9 ojs.values.each { |rate| ratings[rate] += 1 }10 print ratings11 puts ""12 puts "==============================="13 print ojs14 puts ""15 puts "==============================="16 3.times {print "hey!"}17 puts ""18 puts "==============================="19 20 print ojs.length21 puts ""22 print ojs.keys23 puts ""24 print ojs.values25 puts ""26 ojs.keys.each { |name| print name; print " "; print ojs[name]; puts ""; }27 puts "==============================="28 print File.read("x.txt")29 puts "==============================="30 File.open("x.txt", "a") do |f|31 f << "HACKED!\n"32 end33 print File.read("x.txt")34 puts "==============================="35 print File.mtime("x.txt")36 puts ""37 print File.mtime("x.txt").hour38 puts ""39 puts "==============================="
View codeeg2: Create a hash table for reading data from a file
Ruby code
# Read the database of a file and output def load_oj (PATH) ojs ={} file. foreach (PATH) Do | Line | Name, value = line. split (':') ojs [name] = value end print_oj (ojs) enddef print_oj (data) puts "================================" print "Name \ tvalue \ n "data. keys. each do | Name | puts "# {name} \ t # {data [name]}" End puts "============== ==================== "endoj = load_oj (" x.txt ")
View codeeg3: read student information from the file and Output
Ruby code
1 # Read student information from the file and Output 2 class student 3 # attr_accessor: Name 4 # attr_accessor: Number 5 def initialize (name = "unknown", number = "2012309999 ") 6 @ name = Name 7 @ number = Number 8 end 9 def print10 puts "# {@ name} \ t # {@ number}" 11 end12 end13 def load_stu (PATH) 14 Data = {} 15 file. foreach (PATH) Do | Line | 16 Na, NO = line. split ('') 17 S = student. new (No, Na) 18 data [s] = 119 end20 data21 end22 def print_stu (data) 23 puts "================================" 24 print "Name \ tnumber \ n "25 data. keys. each do | Stu | 26 Stu. print27 end28 puts "============================" 29 end30 data = load_stu ("y.txt ") 31 print_stu (data)
View codeeg4: enter four integers in a row to calculate the maximum number of common orders.
Ruby code
# Input four integers in a row to calculate the maximum common approx. Def gcd (A, B) if B = 0 return a else return gcd (B, A % B) endendstr = gets. chompa, B, c, d = Str. split ("") G1 = gcd (. to_ I, B. to_ I); g2 = gcd (C. to_ I, D. to_ I); G3 = gcd (G1, G2) puts "gcd (# {A}, # {B}, # {c}, # {d }) = #{G3 }"