Lesson 2
Filtering Data Streams
Diving Into Filtering Data Streams

Welcome to our hands-on tutorial on data filtering in Ruby.

Filtering is a fundamental skill in programming that lets us focus on specific data we care about while discarding the rest. By filtering, we can extract only the data that meets certain criteria, making it easier to work with relevant information.

Understanding Filtering in Programming

Data filtering in programming is similar to filtering options on a shopping website. Imagine you’re looking for a specific shirt online. You can filter by color, size, brand, and other attributes. In programming, our data items are like those shirts, and our filtering criteria serve as conditions that decide which items pass through.

Filtering Data Using Loops

Loops allow us to repeat actions on each element in a data stream, making them useful for filtering. In Ruby, we can use the each loop to examine each element against certain criteria.

For instance, let’s filter out numbers less than ten from an array:

Ruby
1def filter_with_loops(data_stream) 2 filtered_data = [] 3 data_stream.each do |item| 4 filtered_data << item if item < 10 5 end 6 filtered_data 7end

In this example, the each loop checks each item, and if it’s less than ten, it adds it to the filtered_data array.

Using the select Method for Filtering

Ruby provides a select method specifically for filtering. With select, we can pass a condition block to filter elements in a simpler and more readable way.

Here’s how we could rewrite the previous example using select:

Ruby
1def filter_with_select(data_stream) 2 data_stream.select { |item| item < 10 } 3end

Here, { |item| item < 10 } is the condition that select uses to filter elements in data_stream.

Combining Multiple Conditions with && and ||

In some cases, you may want to filter elements based on more than one condition. In Ruby, we can use && (AND) and || (OR) to combine conditions.

For example, let’s filter for numbers that are both less than ten and even:

Ruby
1data_stream = [23, 5, 7, 12, 19, 2] 2 3filtered_data = data_stream.select { |item| item < 10 && item.even? } 4puts filtered_data # Output: [2]

In this example, only the numbers that are both less than ten and even pass through the filter.

Alternatively, let’s say we want numbers that are either less than ten or greater than twenty:

Ruby
1filtered_data = data_stream.select { |item| item < 10 || item > 20 } 2puts filtered_data # Output: [5, 7, 2, 23]

Here, numbers satisfying either condition (less than ten or greater than twenty) are included in the output.

Applying Filtering Techniques

Now that we understand different filtering techniques, let’s try them out with an example:

Ruby
1data_stream = [23, 5, 7, 12, 19, 2] 2 3# Filtering with loops 4filtered_data_loops = filter_with_loops(data_stream) 5puts "Filtered data with loops: #{filtered_data_loops}" # Output: [5, 7, 2] 6 7# Filtering with select method 8filtered_data_select = filter_with_select(data_stream) 9puts "Filtered data with select method: #{filtered_data_select}" # Output: [5, 7, 2] 10 11# Filtering with combined conditions 12filtered_data_combined = data_stream.select { |item| item < 10 && item.even? } 13puts "Filtered data with combined conditions: #{filtered_data_combined}" # Output: [2]

In each case, we filter the data_stream to extract only the elements that match our criteria.

Lesson Summary

Today, we explored data filtering in Ruby using both loops and the select method, and learned how to combine conditions for more precise filtering. Filtering allows us to refine data streams to focus on the specific elements we need.

Get ready to apply these skills in the upcoming exercises. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.