Created
June 16, 2021 13:56
-
-
Save webuilder240/82e6aadf287b41a75cef7da4e116a407 to your computer and use it in GitHub Desktop.
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 Message | |
attr_reader :id, :text | |
def initialize(id, text) | |
@id = id | |
@text = text | |
end | |
def self.find(messages, search_id) | |
# 2回目以降はインスタンス生成をしたくない。 | |
message = messages.find { |message| message.id == search_id } | |
if message | |
puts "=== Hit #{search_id} ===" | |
return message | |
end | |
tmp = Message.new(search_id, "id_#{search_id}") | |
messages << tmp | |
tmp | |
end | |
end | |
messages = [] | |
100.times do | |
Message.find(messages, (1..100).to_a.sample) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment