Created
March 12, 2014 15:59
-
-
Save xiangzhuyuan/9509887 to your computer and use it in GitHub Desktop.
ruby script lesson-1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student | |
attr_reader :id, :name, :age | |
def initialize(id, name, age) | |
@id = id | |
@name = name | |
@age = age | |
end | |
def id | |
@id | |
end | |
def name | |
@name | |
end | |
def age | |
@age | |
end | |
def to_s | |
"this is No. #{id}:#{name} and age is:#{age}" | |
end | |
end | |
require 'csv' | |
class CSV_reader | |
def initialize | |
@stu = [] | |
end | |
def read_all(filename) | |
#CSV.read(filename) | |
CSV.foreach(filename, headers: true) do |row| | |
@stu << Student.new(row['id'], row['name'], row['age']) | |
end | |
end | |
def get_sum | |
sum = 0 | |
@stu.each { |stu| sum+=1 } | |
sum | |
end | |
def stu | |
@stu | |
end | |
end | |
# | |
#s = Student.new(1, 'xiang', 22) | |
#p s | |
reader = CSV_reader.new | |
ARGV.each do |file| | |
#STDOUT.puts "start reading #{file}...." | |
STDERR.puts "start reading #{file}...." | |
reader.read_all(file) | |
end | |
#reader.read_all('student.csv') | |
p reader.get_sum | |
p reader.stu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment