Question about calling the method

Greetings,

Please see these operations:

irb(main):016:0> 3.+(4)
=> 7
irb(main):017:0> 3 + 4
=> 7

We can call the method with dot (".") or space interval.

But why can't I do this with my own method? My test case:

irb(main):005:0> module Mymod
irb(main):006:1> def echo(a)
irb(main):007:2> a
irb(main):008:2> end
irb(main):009:1> end
=> :echo
irb(main):010:0> s="rena"
=> "rena"
irb(main):011:0> s.extend(Mymod)
=> "rena"
irb(main):012:0> s.echo("kato")
=> "kato"
irb(main):013:0> s echo "kato"
Traceback (most recent call last):
        2: from /usr/bin/irb:11:in `<main>'
        1: from (irb):13
NoMethodError (undefined method `echo' for main:Object)
irb(main):014:0>

Sorry I am newbie to ruby, but a scala background programmer.
In scala I can do:

"katorena" substring (0,4)

res1: String = kato

"katorena".substring(0,4)

res2: String = kato

Can you help check my (maybe wrong) question?
Thank you very much!

Regards,
えりな

Quoting Yamadaえりな (yamoerina@gmail.com):

   Greetings,
   Please see these operations:
   irb(main):016:0> 3.+(4)
   => 7
   irb(main):017:0> 3 + 4
   => 7
   We can call the method with dot (".") or space interval.

You can do that because '+' is an operator. Whose behaviour it is
possible to define, for any class, in the body of a method. But its
ability to be interpreted in

3 + 4

is given by the fact that it is included in the (short) list of
operators. You can see the list by downloading the source of the Ruby
interpreter and looking at file doc/syntax/precedence.rdoc.

   irb(main):013:0> s echo "kato"
   Traceback (most recent call last):
    2: from /usr/bin/irb:11:in `<main>'
    1: from (irb):13
   NoMethodError (undefined method `echo' for main:Object)

'echo' is not an operator. So, in order to use it, you either type the
word by itself, and in this case the interpreter will try to find a
method called echo for the object that is your current scope. Or you
use the '.' operator, that allows tou to indicate to which object the
method you want to invoke belongs.

HTH

Carlo

···

Subject: Question about calling the method
  Date: Sun 09 Jan 22 08:23:07PM +0800

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

irb(main):013:0> s echo "kato"
Traceback (most recent call last):
        2: from /usr/bin/irb:11:in `<main>'
        1: from (irb):13
NoMethodError (undefined method `echo' for main:Object)
irb(main):014:0>

Sorry I am newbie to ruby, but a scala background programmer.
In scala I can do:

> "katorena" substring (0,4)
res1: String = kato

> "katorena".substring(0,4)
res2: String = kato

In ruby, you can pass arguments to methods without brackets.

Therefore if this were "valid ruby” (in the way you desire), it would be ambiguous:

s echo “kato"

Does that mean:

s(echo, “kato”)
or
s.echo(“kato”)

?

···

On 9 Jan 2022, at 12:23, Yamadaえりな <yamoerina@gmail.com> wrote: