Welcome to the practical segment of our Ruby programming journey! Today, we're applying the knowledge from past lessons to solve two practice problems using advanced Ruby data structures: queues
and sorted arrays with custom class keys
.
Consider an event-driven system, like a restaurant. Orders arrive, and they must be handled in the order they were received, following the First In, First Out (FIFO) principle. This principle makes it a perfect scenario for a queue
implementation in Ruby using arrays.
Ruby1class Queue 2 def initialize 3 # Initializing an empty queue 4 @buffer = [] 5 end 6 7 def enqueue(val) 8 # Adding (enqueueing) an item to the queue 9 @buffer.push(val) 10 end 11 12 def dequeue 13 # Removing (dequeuing) an item from the queue 14 @buffer.shift 15 end 16 17 # Checking if the queue is empty 18 def is_empty 19 @buffer.empty? 20 end 21 22 # Checking the size (number of items) in the queue 23 def size 24 @buffer.size 25 end 26end
This code demonstrates the creation and operation of a Queue
class, which leverages Ruby's array to efficiently implement a queue. The Queue
class includes methods to enqueue
(add) an item, dequeue
(remove) an item, check if the queue is empty, and return the queue's size. Enqueue operations add an item to the end of the array, while dequeue operations remove an item from the beginning, maintaining the First In, First Out (FIFO) principle.
In our implementation of a queue
using Ruby arrays, the enqueuing of an item is done by using the push
method, mimicking the arrival of a new order at the restaurant. The dequeuing serves an order using the shift
method, reflecting the preparation and delivery of the order in FIFO order.
For the second problem, envision a leaderboard for a video game. Players with their scores can be represented as objects of a custom class
, then sorted efficiently. In Ruby, this can be achieved using arrays paired with Ruby's sort
method and the Comparable
module.
Ruby1class Player 2 include Comparable 3 4 attr_reader :name, :score 5 6 def initialize(name, score) 7 @name = name 8 @score = score 9 end 10 11 # Defining comparator for custom class 12 def <=>(other) 13 [other.score, name] <=> [@score, other.name] 14 end 15 16 # Defining the string representation of Player object 17 def to_s 18 "(#{name}, #{score})" 19 end 20end 21 22# Create an array of players 23players = [ 24 Player.new("John", 900), 25 Player.new("Doe", 1000) 26] 27 28# Sort players 29sorted_players = players.sort 30 31# Print sorted players 32sorted_players.each do |player| 33 puts player # e.g., (Doe, 1000) 34end
This code snippet introduces a Player
class for representing players in a video game, incorporating the Comparable
module to allow sorting by score (primary) and name (secondary). An array of Player
instances is then sorted, ensuring that players are stored in a manner that is sorted first by their scores and then by their names if scores are equal. This is essential for functionalities like leaderboards, where players need to be ranked efficiently according to their performance.
In our leaderboard system, we used a Player
custom class with the Comparable
module for sorting. We stored instances of Player
in an array and used the sort
method to order them. This allows us to rank player scores effectively, illustrating a common requirement in a competitive gaming system.
We've successfully employed Ruby's built-in data structures — queues
with arrays and sorted arrays using Comparable
— to solve real-world problems. This hands-on approach enables us to better understand these concepts. More exercises to cement your understanding are coming up next. Happy coding!