Search code examples
rubyclassobjectnamespacesclass-method

I'm having trouble calling methods belonging to certain classes within methods of an alternate class


This is my first time asking a question so if I could be doing something better please let me know.

I have three classes, all which need to be working together. Here's an example:

class CLI

def self.start
   s=Scraper.new
    puts "Welcome to your basic music theory coordinator!"
    puts ""
    puts "If you want to check out a key, choose from the list below by typing the key as you see it listed."
    puts "*not yet functional* If you want to generate a random chord progression in a certain key, just pick the key from the list and say generate"
    puts ""

    puts "Pick a key:"
    puts " "
    puts "Major:"
    puts s.all_scale_names[0]
    puts " "
    puts "Minor:"
    puts s.all_scale_names[1]

      s.key_information_creator
   end
end

CLI.start

The errors I get when trying to call CLI.start are as follows:

9: from lib/comman_line_interface.rb:1:in `<main>'
        8: from lib/comman_line_interface.rb:1:in `require_relative'
        7: from /home/code/cli-test/test-cli/lib/scraper.rb:4:in `<top (required)>'
        6: from /home/code/cli-test/test-cli/lib/scraper.rb:4:in `require_relative'
        5: from /home/code/cli-test/test-cli/lib/song.rb:5:in `<top (required)>'
        4: from /home/code/cli-test/test-cli/lib/song.rb:5:in `require_relative'
        3: from /home/code/cli-test/test-cli/lib/key.rb:6:in `<top (required)>'
        2: from /home/code/cli-test/test-cli/lib/key.rb:6:in `require_relative'
        1: from /home/code/cli-test/test-cli/lib/comman_line_interface.rb:31:in `<top (required)>'
/home/code/cli-test/test-cli/lib/comman_line_interface.rb:12:in `start': uninitialized constant CLI::Scraper (NameError)

I think I need to be doing something called namespacing, but I'm not entirely sure. Some direction or options for how to fix this and get my classes working together well would be greatly appreciated. Thanks.


Solution

  • Understanding Your Stack Trace

    Your stack trace is pretty straightforward. It's telling you:

    1. You haven't successfully required your Scraper class/module.
    2. There's no CLI::Scraper defined in your current namespace.
    3. You're invoking methods on objects that aren't in scope or that don't exist in your code at all.

    Steps to Debug Your Code

    Address the issues above, and see where that gets you. Off the cuff, there a few concrete things you should consider as you debug your program:

    1. Your CLI class has no way to reference a Scraper class/module outside its namespace unless you tell it where to look through importing modules or namespacing them within a common module so you can reference Foo::Scraper and Foo::CLI explicitly.

    2. Your probably better off injecting a Scraper into your CLI initialization anyway, but you haven't shown enough code to offer a meaningful example.

    3. Strip away all non-essential code until you can get the basics working. For example:

      class CLI
        def self.start
          defined? Scraper
        end
      end
      
    4. Keep refactoring until your minimal CLI#start returns "constant" instead of nil.