super_J
super_J

Reputation: 234

Ruby file won't load for me

I'm taking a coding class and ran into a problem while trying to code along with the instructor. I am trying to learn the part about mixins and the Enumerable module, and made this file, just like the instructor did, but it's not working for me. I have Ruby installed and have been successfully using RVM to manage Ruby and Rails versions so far, so I don't understand why this isn't working for me. Any help at all would be greatly appreciated.

Here is the screencaps of the errors I get in my terminal:

enter image description here

enter image description here

And here is my sample code:

     # Enumerable as a mixin

     class ToToList
       include 'Enumerable'

       attr_accessor :items, :finished_items

       def initialize
         @items = []
       end

       def each
         items.each {|item| yield item}
       end
     end

     # list = ToToList.new
     # list.items = ['laundry', 'dishes', 'vacuum']
     # list.items.select {|i| i.length > 6}

Upvotes: 1

Views: 654

Answers (2)

super_J
super_J

Reputation: 234

I found the problem. Apparently, in newer versions of Ruby (I am currently using Ruby v. 2.1.2 p95), the single quotes around Enumerable in the include statement is what made it not work correctly. After conferring with the manual at ruby-docs, I saw this code sample: include Enumerable and noticed that there were no single quotes around it. So I removed the single quotes and it now works fine.

Upvotes: 1

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 18567

require will look for a file called to_do_list.rb on your $LOAD_PATH. Chances are if you've got some git repo and that's your working directory for your project, it won't be on your $LOAD_PATH. Usually $LOAD_PATH mostly has just standard library stuff.

You can either use require passing it the full (absolute) path to your to_do_list.rb file, or use require_relative and pass it the path to said file relative to your current location (wherever you invoked irb from).

Upvotes: 0

Related Questions