file io - How do I read data line by line in Ruby? -
i new ruby , creating poker game application. trying read in each player's cards text file. data in text file looks this:
8c ts kc 9h 4s 7d 2s 5d 3s ac 5c ad 5d ac 9c 7c 5h 8d td ks 3h 7h 6s kc js qh td jc 2d 8s
and forth. first 5 on each line 1 player's cards, , other 5 other's. how go reading in data? have written card class can create cards based on how represented in file.
io.foreach('cards.txt') |line| cards = line.chomp.split.map { |c| card.new(c) } hand1 = cards[0, 5] hand2 = cards[5, 5] #... end
or...
handpairs = file.open('cards.txt') |f| f.each_line.map { |line| line.chomp.split.each_slice(5).map { |cs| cs.map { |c| card.new(c) } } } end
Comments
Post a Comment