Welcome to the lesson on data streams. Data streams represent continuous datasets, much like data received in real-time from a weather station or a gaming application.
In this lesson, we will explore handling these data streams, learning to access elements, slice segments, and convert these streams into strings for better comprehension.
In Ruby, data streams are typically represented using arrays, with each element potentially being a hash to store structured data.
Let's create a simple Ruby class named DataStream
. This class will encapsulate operations related to data streams in our program:
Ruby1class DataStream 2 def initialize(data) 3 @data = data 4 end 5end
To use it, we instantiate a DataStream
object with an array, where each element is a hash:
Ruby1stream = DataStream.new([ 2 { id: 1, value: 100 }, 3 { id: 2, value: 200 }, 4 { id: 3, value: 300 }, 5 { id: 4, value: 400 } 6])
To access individual elements in a data stream, indexing is commonly used. The get
method shown below fetches the i
-th element from the data stream:
Ruby1class DataStream 2 def initialize(data) 3 @data = data 4 end 5 6 def get(i) 7 @data[i] 8 end 9end
Here's how you use the get
method:
Ruby1stream = DataStream.new([ 2 { id: 1, value: 100 }, 3 { id: 2, value: 200 }, 4 { id: 3, value: 300 }, 5 { id: 4, value: 400 } 6]) 7 8puts stream.get(2) # Outputs: {:id=>3, :value=>300} 9puts stream.get(-1) # Outputs: {:id=>4, :value=>400}
In this example, stream.get(2)
retrieves {:id=>3, :value=>300}
, the third element (since indexing starts from 0
). Meanwhile, stream.get(-1)
retrieves the last element, {:id=>4, :value=>400}
. In Ruby, you can also access the first and last elements in an array directly using .first
and .last
.
Slicing retrieves a range of elements rather than a single one. The slice
method creates a new array containing elements from position i
to j
(inclusive) in the data stream:
Ruby1class DataStream 2 def initialize(data) 3 @data = data 4 end 5 6 def get(i) 7 @data[i] 8 end 9 10 def slice(i, j) 11 @data[i..j] 12 end 13end
Here's how you use the slice
method:
Ruby1stream = DataStream.new([ 2 { id: 1, value: 100 }, 3 { id: 2, value: 200 }, 4 { id: 3, value: 300 }, 5 { id: 4, value: 400 } 6]) 7 8puts stream.slice(1, 3) # Outputs: [{:id=>2, :value=>200}, {:id=>3, :value=>300}, {:id=>4, :value=>400}]
In this example, stream.slice(1, 3)
retrieves an array with the elements at positions 1 to 3.
To improve readability, converting data streams into strings can be beneficial. We can provide a custom string representation of our data elements using the to_string
method:
Ruby1class DataStream 2 def initialize(data) 3 @data = data 4 end 5 6 def get(i) 7 @data[i] 8 end 9 10 def slice(i, j) 11 @data[i..j] 12 end 13 14 def to_string 15 '[' + @data.map(&:to_s).join(', ') + ']' 16 end 17end
Here, we use map
to transform each element in the data stream into a string format for display purposes. We’ll cover map
in depth in a future unit to explore its powerful capabilities for working with collections.
Here's the to_string
method at work:
Ruby1stream = DataStream.new([ 2 { id: 1, value: 100 }, 3 { id: 2, value: 200 }, 4 { id: 3, value: 300 }, 5 { id: 4, value: 400 } 6]) 7 8puts stream.to_string # Outputs: [{:id=>1, :value=>100}, {:id=>2, :value=>200}, {:id=>3, :value=>300}, {:id=>4, :value=>400}]
In this example, to_string
provides a human-readable string representation of all elements in the data stream.
This lesson covered data streams, demonstrating how to represent and manipulate them using Ruby's native structures like arrays and hashes. We encapsulated operations on data streams within a Ruby class, providing mechanisms for element access, slicing, and conversion to string representations. Now, you're ready to practice with the exercises that follow!