DEV Community

pooyaalamdari
pooyaalamdari

Posted on

case / when

class Ticket
    attr_accessor :venue, :date
    def initialize(venue, date)
        self.venue = venue
        self.date = date
    end

    def ===(other_ticket)
        self.venue == other_ticket.venue
    end
end

ticket1 = Ticket.new("town hall", "01/01/2022")
ticket2 = Ticket.new("conference", "01/01/2022")
ticket3 = Ticket.new("town hall", "08/05/2000")
puts "ticket1 is for an event at : #{ticket1.venue}"

case ticket1 
when ticket2
    puts "same location as ticket2"
when ticket3
    puts "same location as ticket3"
else 
    puts "no match!"
end
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)