Why can't I call my method following a period


I’m toying with format on the lesson:


I am getting this error when I try to call my method using “8.cubertino”:

undefined method `cubertino’ for 8:Fixnum

def cubertino(n)
  puts n ** 3



The above assumes that the number class has a method, cubertino, which in likelihood, it does not, at least not in the standard core.

A method takes its context from the instance that invokes it. A function in a global context depends upon parameters or global variables for access to data.

def cubertino(n)
  puts n ** 3

puts cubertino(8).is_a? Integer

puts 8.cubertino
undefined method `cubertino' for 8:Fixnum

In the above error message we see that 8 inherits from Fixnum class, which does not have the method we invoked.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.