Skip to content

Instantly share code, notes, and snippets.

@xiangzhuyuan
Created March 12, 2014 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiangzhuyuan/9509887 to your computer and use it in GitHub Desktop.
Save xiangzhuyuan/9509887 to your computer and use it in GitHub Desktop.
ruby script lesson-1
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