标签:style blog http color os 文件
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 ratings 11 puts "" 12 puts "===============================" 13 print ojs 14 puts "" 15 puts "===============================" 16 3.times {print "hey!"} 17 puts "" 18 puts "===============================" 19 20 print ojs.length 21 puts "" 22 print ojs.keys 23 puts "" 24 print ojs.values 25 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 end 33 print File.read("x.txt") 34 puts "===============================" 35 print File.mtime("x.txt") 36 puts "" 37 print File.mtime("x.txt").hour 38 puts "" 39 puts "==============================="
Ruby Code
# 读取一个文件的数据库并且输出 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")
Ruby Code
1 # 从文件中读取学生信息并且输出 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 print 10 puts "#{@name}\t#{@number}" 11 end 12 end 13 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] = 1 19 end 20 data 21 end 22 def print_stu( data ) 23 puts "================================" 24 print "name\tnumber\n" 25 data.keys.each do |stu| 26 stu.print 27 end 28 puts "================================" 29 end 30 data = load_stu("y.txt") 31 print_stu(data)
Ruby Code
# 一行内输入4个整数,计算这四个数的最大公约数 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}"
【Ruby】Ruby大法第一天,布布扣,bubuko.com
标签:style blog http color os 文件
原文地址:http://www.cnblogs.com/polossk/p/3857653.html