require_relative and utility methods

765 views Asked by At

I wanted to separate some methods into a module for abstraction purposes but I am getting a nomethod error when testing the first function in my module.

functions.rb

module Functions

  def avg_ticket(vol,count)
    (vol.to_f/count).round(2)
  end

end

example.rb

require_relative 'functions'
vol = 5000
count = 2500
avg_ticket = Functions.avg_ticket(vol,count)

I am getting a undefined method 'avg_ticket' for functions:Module (NoMethodError)

both files are in the same folder so I am using require_relative if that makes a difference. this may be basic, but can anyone help me understand why the method is undefined here?

edit: changed module functions to module Functions in question

1

There are 1 answers

2
Nick Veys On BEST ANSWER

You named your module functions, but you're trying to call Functions. Names are case sensitive. Also, you need to name your module with an upper case first letter anyway. Also, to be defined on the module itself, you want to use def self.avg_ticket, see the following:

module Functions

  def self.avg_ticket(vol,count)
    (vol.to_f/count).round(2)
  end

end

And using it:

p Functions.avg_ticket(2, 25)
> 0.08