Why can't I call my method following a period


#1

I’m toying with format on the lesson:

https://www.codecademy.com/courses/learn-ruby/lessons/methods-blocks-sorting/exercises/parameters-and-arguments?action=lesson_resume&link_content_target=interstitial_undefined

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
end

8.cubertino 



#2

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
end

puts cubertino(8).is_a? Integer

puts 8.cubertino
512
true
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.


#3

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