CJG
CJG

Reputation: 558

Name error when creating empty set in Ruby

I'm trying to use the Set class from the standard Ruby library. I am creating an empty Set like so:

Set.new

but then I end up with this error:

main.rb:36:in `<main>': uninitialized constant Set (NameError)

Set.new
^^^

How do I properly create an empty instance of Set

Upvotes: 0

Views: 163

Answers (2)

Scott Milella
Scott Milella

Reputation: 487

You need to add

require 'set'

Top the top of your Ruby file main.rb

Upvotes: 2

mechnicov
mechnicov

Reputation: 15288

Set is standard Ruby class, but you need to require it just like any other class / module from the standard library (like JSON, Date, Timeout, etc.)

You need to add to the top of your main.rb

require 'set'

You can see it in the examples in the official docs

Upvotes: 6

Related Questions