Skip to content

Instantly share code, notes, and snippets.

@alto
Created October 3, 2012 08:12
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 alto/3825749 to your computer and use it in GitHub Desktop.
Save alto/3825749 to your computer and use it in GitHub Desktop.
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