Q:

Very confused by this use of 'self'; 'parameter.method'

Code below; this is an example from textbook. Questions:

Why is it 'puts 10.miles.back'? Since that line is outside the class Numeric, shouldn't it be 'puts Numeric.10.miles.back'?
I've read that 'self' refers to the containing class. So how is it that it seems to be acting as a number here?
If I remove 'self *' from the first method called, the code still seems to work exactly the same. So what is it doing there?
I can't seem to convert any of these methods to a normal method which accepts a parameter. It outputs an error reporting that the method is undefined.
I'm guessing that number.method is a way of passing parameters. What is this syntax called?

class Numeric
  def inches
    self
  end

  def feet
    self * 12.inches
  end

  def yards
    self * 3.feet
  end

  def miles
    self * 5280.feet
  end

  def back
    self * -1
  end

  def forward
    self
  end
end

puts 10.miles.back

A:

In ruby, basically everything is an instance of a class. In this case, a number literal is an instance of Numeric. This code re-opens up that class and adds methods to it. This gives all numbers in your program these methods.

No. 10 is an instance of Numeric, and therefore has all the public methods just added, including a miles method. 10.miles calls the miles method of 10, which you just added.
"I've read that 'self' refers to the containing class." That's not accurate. self refers to the object whose method is being executed. When you call "a,b,c".split(','), that would call the split method on the string object, and inside that method self would refer to the string "a,b,c".
There is no self * in the first method, but if you mean the self * in def miles, then it's just multiplication. Remember, self in these methods is a number. So 10.miles results in 10 * 5280.feet, which returns a number representing 10 miles.
Adding an argument should work fine, so long as you provide an argument when you call it:

class Numeric
  def miles(arg)
    puts "got an arg #{arg}"
    self * 5280.feet
  end
end

10.miles("some argument")

number.method is a way of invoking (or calling) methods. 10.miles.back works because you call the miles method on the number 10, which returns a different number. Then you call the back method on that number.

