Created
October 3, 2012 08:12
-
-
Save alto/3825749 to your computer and use it in GitHub Desktop.
Adding to_s to classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def to_s | |
"I am a Foo." | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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