Tsallaka zuwa abun ciki
Justice Tech School

Ranges and Sets

Ranges represent sequences, and to_a expands them. Set stores unique values with union, intersection and difference.

Example
require "set"
r = (1..10)
puts r.step(3).to_a.inspect, r.sum, r.include?(5)
s = Set.new([1, 2, 3])
t = Set[3, 4]
puts (s | t).to_a.inspect, (s & t).to_a.inspect, (s - t).to_a.inspect

Result

[1, 4, 7, 10]
55
true
[1, 2, 3, 4]
[3]
[1, 2]

Exercise

Expand a range.

(1..3).

Quiz Test

Which keeps unique values?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.