Skip to content

Instantly share code, notes, and snippets.

@orend
Created April 8, 2014 14:38
Show Gist options
  • Save orend/10134763 to your computer and use it in GitHub Desktop.
Save orend/10134763 to your computer and use it in GitHub Desktop.
further refactoring to "Using a Ruby Class To Write Functional Code" : refactoring http://patshaughnessy.net/2014/4/8/using-a-ruby-class-to-write-functional-code
class Line
attr_reader :Line
def initialize(text)
@line = text
end
def office
values[2].strip
end
def employee_id
values[1].strip
end
def last_name
name = values[0]
name.split[0]
end
def to_s
"employee_id: #{employee_id}\n" <<
"office: #{office}\n" <<
"last_name: #{last_name}"
end
private
def values
@values ||= @line.split(',')
end
end
def parse3(lines)
lines.map do |text|
Line.new(text)
end.each do |line|
puts "employee_id: #{line.employee_id}"
puts "office: #{line.office}"
puts "last_name: #{line.last_name}"
end
end
# using to_s
def parse4(lines)
lines.map do |text|
Line.new(text)
end.each do |line|
puts line.to_s
end
end
# building the entire string at once
def parse5(lines)
puts lines.map { |text| Line.new(text) }.map(&:to_s).join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment