Home > Net >  in `_main': undefined method `run' for nil:NilClass (NoMethodError)
in `_main': undefined method `run' for nil:NilClass (NoMethodError)

Time:05-06

I can't call the run method in a class called MySqliteRequest. When I call the method,the error is going out.

in `main': undefined method `run' for nil:NilClass (NoMethodError)

Here some methods of MySqliteRequest

class MySqliteRequest
    def print
        puts "Type Of Request #{@type_of_request}"
    end

    def run
        print
    end

    def _setTypeOfRequest(new_type)
        if(@type_of_request == :none or @type_of_request == new_type)
            @type_of_request = new_type
        else
            raise "Invalid: type of request already set #{@type_of_request} (new type => #{new_type}"
        end
    end
end

And main method ↓

def _main()
   request = MySqliteRequest.new
   request = request.from('nba_player_data.csv')
   request = request.select('name')
   request = request.where('birth_state', 'Indiana')
   request.run
end
_main()

CodePudding user response:

At the point where you call request.run the value for request is nil. This is why you are seeing the error you're given.
This is happening because the line right above it assigns the nil value to the request variable.

You are clearly coming from another language that is not Ruby (some type of C maybe?), by how you've formatted things. It would help for you to get more familiar with Ruby and its idioms. However, best I can tell, you want to do something like this instead:

def _main
   request = MySqliteRequest.new
   request.from('nba_player_data.csv')
   request.select('name')
   request.where('birth_state', 'Indiana')
   request.run
end
_main()

This assumes you've also defined (and in some cases probably overridden) these methods on your MySqliteRequest Object or Model:
from
select
where

However, please note that the way you're going about this is just completely against how Ruby and Ruby on Rails is designed to work.

  • Related