Skip to content

Instantly share code, notes, and snippets.

@alto alto/job.rb
Created Oct 3, 2012

Embed
What would you like to do?
Adding to_s to classes
class Job
def initialize(items_to_process)
@items = items_to_process
@timestamp = Time.now
# start_processing in the background...
end
def to_s
if finished?
"All work is done, all #{@items.size} items are processed."
else
"Still working (#{Time.now - @timestamp}s), \
#{@processed_items.size} of #{@items.size} processed."
end
end
end
ruby> job = Job.new([item1, item2, ...]
ruby> puts job
Still working (2s), 2 of 100 items processed.
ruby> puts job
Still working (4s), 7 of 100 items processed.
...
ruby> puts job
All work is done, all 100 items are processed.
class Foo
def to_s
"I am a Foo."
end
end
ruby> foo = Foo.new
ruby> puts foo
I am a Foo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.