Skip to content

Instantly share code, notes, and snippets.

@johnkary
Last active August 29, 2015 13:56
Show Gist options
  • Save johnkary/8930002 to your computer and use it in GitHub Desktop.
Save johnkary/8930002 to your computer and use it in GitHub Desktop.
Response code to Use An Ask, Don't Tell Policy With Ruby - http://patshaughnessy.net/2014/2/10/use-an-ask-dont-tell-policy-with-ruby
class Poem
def initialize(lines)
@lines = lines
end
def lines_after_functional(target)
target_index = (0..@lines.size-1).detect do |i|
@lines[i].include?(target)
end
target_index ? @lines[target_index..-1] : []
end
def lines_after_imperative(target)
result = []
flag = false
@lines.each do |line|
flag = flag || line.include?(target)
result << line if flag
end
result
end
end
text = File.open('poem.txt').readlines
poem = Poem.new(text)
puts poem.lines_after_functional 'glimmer'
puts poem.lines_after_imperative 'glimmer'
# There midnight's all a glimmer, and noon a purple glow,
# And evening full of the linnet's wings.
# I will arise and go now, for always night and day
# I hear lake water lapping with low sounds by the shore;
# While I stand on the roadway, or on the pavements grey,
# I hear it in the deep heart's core.
I will arise and go now, and go to Innisfree,
And a small cabin build there, of clay and wattles made:
Nine bean-rows will I have there, a hive for the honeybee,
And live alone in the bee-loud glade.
And I shall have some peace there, for peace comes dropping slow,
Dropping from the veils of the morning to where the cricket sings;
There midnight's all a glimmer, and noon a purple glow,
And evening full of the linnet's wings.
I will arise and go now, for always night and day
I hear lake water lapping with low sounds by the shore;
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart's core.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment