Skip to content

Instantly share code, notes, and snippets.

@ka8725
Created April 12, 2020 00:55
Show Gist options
  • Save ka8725/4fa4e94b059a9b1f7c4fe5393fa7e850 to your computer and use it in GitHub Desktop.
Save ka8725/4fa4e94b059a9b1f7c4fe5393fa7e850 to your computer and use it in GitHub Desktop.

Revisions

  1. ka8725 created this gist Apr 12, 2020.
    30 changes: 30 additions & 0 deletions ruby_closure.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    class SMSGateway
    # @param phone [String]
    # @param message [String]
    def self.send_message(phone, message)
    puts "Hello #{phone}, #{message}"
    end
    end

    User = Struct.new(:phone, :active)

    class MessageService
    # @param message [String]
    # @param recipients [Array<User>]
    def broadcast(message, recipients)
    recipients.each(&send_message(message))
    end

    private

    def send_message(message)
    ->(recipient) { SMSGateway.send_message(recipient.phone, message) if recipient.active}
    end
    end

    recipients = [
    User.new('+12345654547', true),
    User.new('+12345654548', false),
    ]
    service = MessageService.new
    service.broadcast('have a good day!', recipients)