Where Is Method Call Precedence?

Hi,

When I look at “Table 18.4: Ruby Operators” in the Pickaxe book, I cannot
find the parentheses and the “dot” that are used for method call, and the
parentheses for grouping themselves. Well, Table 18.4 really says
"operators", but I am really wondering in the complete Ruby precedence,
where are the places for:

- parentheses for method call
- the dot for method call
- parentheses for grouping

?

Regards,

Bill

Hi,

···

In message “Where Is Method Call Precedence?” on 02/09/27, William Djaja Tjokroaminata billtj@y.glue.umd.edu writes:

When I look at “Table 18.4: Ruby Operators” in the Pickaxe book, I cannot
find the parentheses and the “dot” that are used for method call, and the
parentheses for grouping themselves. Well, Table 18.4 really says
“operators”, but I am really wondering in the complete Ruby precedence,
where are the places for:

  • parentheses for method call
  • the dot for method call
  • parentheses for grouping

Usually these are not handled by precedence order. They are just
different rules in the syntax (parhaps except for dot). The dot is
the strongest “operator”.

						matz.

Hi,

This is actually rather confusing to me, since in Ruby the ‘()’ is
optional in a method call. In all other languages that I have used, the
‘()’ is almost always mandatory (I never used Eiffel).

My question is, what is the reason of not to put some space before the
opening ‘(’ in a method call? I personally have not encountered a
pathological case where the space does create a problem.

Regards,

Bill

···

============================================================================
Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

Usually these are not handled by precedence order. They are just
different rules in the syntax (parhaps except for dot). The dot is
the strongest “operator”.

  					matz.

Fri, 27 Sep 2002 23:02:25 +0900, William Djaja Tjokroaminata billtj@z.glue.umd.edu pisze:

This is actually rather confusing to me, since in Ruby the ‘()’ is
optional in a method call. In all other languages that I have used,
the ‘()’ is almost always mandatory (I never used Eiffel).

http://merd.net/pixel/language-study/syntax-across-languages.html#FnctnFnctCall

Function call with no parameter:

f Ada, Eiffel, Haskell, Mercury, Perl, Pliant, Ruby, Tcl, Pascal
f() Awk, C, C#, C++, Java, Lua, Python
f() merd, OCaml, SML (there really is a parameter which is the empty tuple)

It might not matter much for standalone functions, but if method call
looks like this:
x.f(y,z)
then it’s nice for parameterless methods and field access to use the
same syntax so code doesn’t need to know if it’s a field or method:
x.f
and Ruby indeed does it by wrapping field accessors in methods with
a syntax which looks like field access.

So this is a reason against these empty parens. But here is a
problem with this approach: in languages with first-class functions
the function call can’t be simply an expression consisting of the
expression denoting the function and expressions denoting arguments,
because the function name itself already denotes the call with no
arguments.

So taking the function without calling it requires writing something
extra to wrap it in an object, and calling a function passed in a
variable looks differently than calling an explicitly named function.

The last paragraph doesn’t apply to Haskell despite the lack of (),
but it’s quite a different story.

···


__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

“Marcin ‘Qrczak’ Kowalczyk” qrczak@knm.org.pl writes:

http://merd.net/pixel/language-study/syntax-across-languages.html#FnctnFnctCall

small note:

page …/syntax-across-languages.html can now be accessed per category
via …/syntax-across-languages/, so one gets:

http://merd.net/pixel/language-study/syntax-across-languages/Fnctn.html#FnctnFnctCall
(8KB vs 116KB)

happy reading :slight_smile:

···


Pixel
programming languages addict http://merd.net/pixel/language-study/

“Marcin ‘Qrczak’ Kowalczyk” qrczak@knm.org.pl wrote in message

f Ada, Eiffel, Haskell, Mercury, Perl, Pliant, Ruby, Tcl, Pascal
f() Awk, C, C#, C++, Java, Lua, Python
f() merd, OCaml, SML (there really is a parameter which is the empty
tuple)

You can also add OCaml to the first line but in this case the function would
only ever be evaluated once - henceforth the return value would be used
(strict evaluation).

Be careful with OCaml and parentheses:

For the sake of example lets define the min and max functions first:
let min a b = a < b then a else b;; let max a b = a > b then a else b;;

OCaml does not have parentheses around function arguments. arguments follow
the function separated by space: “min 2 3” this yields “2” as expected.

C-style “min(x, max(y, z))” becomes “min x (max y z)” in OCaml. The
parentheses are used to correctly group the arguments. Really “(max y z)”
means the single value tuple made up of the return value from max - but a
single value is the same as the single value tuple so the parentheses works
like normal parentheses grouping.

“min(2,3)” yields something strange - a partially evaluated function taking
a two integer tuple as argument as if it was defined like
“let min_strange (x, y) = if (2, 3) < (x, y) then (2, 3) else (x, y);;”
(you can compare tuples, so the function makes sense).

You could also define min to work on a two tuple:
“let min’ (a, b) = a < b then a else b” and you would get the expected
result when writing “min(2,3)”. This would be the same as writing a Ruby
function expecting an array as argument to a min function.

It’s correct that the empty tuple () can be applied to a function. But a
function can also be called without arguments at al (depending on its type).
A function without arguments evaluate to a constant and is only executed
once. There you sometimes write functions that take dummy arguments such as
the empty tuple to allow multiple evalutions (which is useful when printing,
say, linebreaks to an out stream).

The arguments are applied to OCaml differs from standard ML (SML) and it is
primary source of trouble to newcomers (i.e. me). (Someone said OCaml is big
on currying or something).

There is a reason for OCamls strange syntax: partially evaluated functions
and higher order functions (functions taking functions as arguments) becomes
very natural to work with.

If a function isn’t provided with all arguments, the return value is a new
function that accepts the remaining arguments: “(min 2)” yields a new
function taking one integer and returns the smaller value of 2 and the
argument: “(min 2) 4” is the same as “min 2 4” but it happened was evaluated
in two steps (in bytecode this may matter, in assembly it’s optimized away).

In conclusion: In OCaml you generally put the left parentheses before the
function name, rather than after it. This may look at bit like Lisp, but
isn’t quite.

Tuples are very powerful datastructures in OCaml. They are not lists and
they are not arrays. They do, for example, make it much easier to handle
values in parser reductions. In Ruby you would use arrays or hashes, but
they have a much higher overhead. It would be nice with tuples in Ruby.

Mikkel

Hi,

···

In message “Re: Where Is Method Call Precedence?” on 02/09/27, William Djaja Tjokroaminata billtj@z.glue.umd.edu writes:

My question is, what is the reason of not to put some space before the
opening ‘(’ in a method call? I personally have not encountered a
pathological case where the space does create a problem.

It’s due to YACC restriction. It is relaxed in 1.7.x though.

						matz.

Since Ruby is close to Python is some ways, and Python has tuples, I cast the
question: “What are tuples used for in Python?”. If someone answers that, then
I would get an idea of why I might like them in Ruby.

Cheers,
Gavin

···

----- Original Message -----
From: “MikkelFJ” mikkelfj-anti-spam@bigfoot.com

Tuples are very powerful datastructures in OCaml. They are not lists and
they are not arrays. They do, for example, make it much easier to handle
values in parser reductions. In Ruby you would use arrays or hashes, but
they have a much higher overhead. It would be nice with tuples in Ruby.

Mikkel

Hi,

If I remember correctly, tuples can be used as hash keys while an
ordinary array (or ‘list’ as Python calls it) cannot. Python has the
concept of mutable and immutable objects. Only immutable objects can be
used as hash (or ‘dictionary’ as Python calls it) keys.

I don’t know why Python needs the concept of mutable and immutable objects
while Ruby does not. I guess this is related to the fundamental object
model. I think I understand the Ruby object model, but I never got a
chance to peek under the hood of Python.

Regards,

Bill

···

==============================================================================
Gavin Sinclair gsinclair@soyabean.com.au wrote:

Since Ruby is close to Python is some ways, and Python has tuples, I cast the
question: “What are tuples used for in Python?”. If someone answers that, then
I would get an idea of why I might like them in Ruby.

Cheers,
Gavin

“Gavin Sinclair” gsinclair@soyabean.com.au wrote in message
news:045901c266f5$19ba5de0$845186cb@nosedog…

From: “MikkelFJ” mikkelfj-anti-spam@bigfoot.com

Tuples are very powerful datastructures in OCaml. They are not lists and
they are not arrays. They do, for example, make it much easier to handle
values in parser reductions. In Ruby you would use arrays or hashes, but
they have a much higher overhead. It would be nice with tuples in Ruby.

Mikkel

Since Ruby is close to Python is some ways, and Python has tuples, I
cast the
question: “What are tuples used for in Python?”. If someone answers that,
then
I would get an idea of why I might like them in Ruby.

I was rethinking this. I’m not sure tuples would make any difference
compared to Rubys current arrays. It’s more a question of ensuring that Ruby
internally handles small arrays efficiently:
[1,2,3] should be allocated in memory like <type=small-array, size=3, 1, 2,
3>. I.e. approximately the same overhead as floating values. Once a small
array is mutated with push etc. they could be converted into a more generic
array datatype internally. I don’t know how it currently works, but I think
the current array type is sufficient.

Mikkel

···

----- Original Message -----

Hello Gavin,

Saturday, September 28, 2002, 5:45:05 PM, you wrote:

Tuples are very powerful datastructures in OCaml. They are not lists and
they are not arrays. They do, for example, make it much easier to handle
values in parser reductions. In Ruby you would use arrays or hashes, but
they have a much higher overhead. It would be nice with tuples in Ruby.

Since Ruby is close to Python is some ways, and Python has tuples, I cast the
question: “What are tuples used for in Python?”. If someone answers that, then
I would get an idea of why I might like them in Ruby.

tuple is an array with fixed size. when you have very large number of
small arrays, you will save many memory bytes

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi,

I think Ruby NArray has about the same efficiency as the new array/tuple
that you describe, although it seems that NArray does not have an explicit
push method.

Regards,

Bill

···

============================================================================
MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

I was rethinking this. I’m not sure tuples would make any difference
compared to Rubys current arrays. It’s more a question of ensuring that Ruby
internally handles small arrays efficiently:
[1,2,3] should be allocated in memory like <type=small-array, size=3, 1, 2,
3>. I.e. approximately the same overhead as floating values. Once a small
array is mutated with push etc. they could be converted into a more generic
array datatype internally. I don’t know how it currently works, but I think
the current array type is sufficient.

Hello William,

Saturday, September 28, 2002, 7:28:03 PM, you wrote:

Hi,

If I remember correctly, tuples can be used as hash keys while an
ordinary array (or ‘list’ as Python calls it) cannot. Python has the
concept of mutable and immutable objects. Only immutable objects can be
used as hash (or ‘dictionary’ as Python calls it) keys.

I don’t know why Python needs the concept of mutable and immutable objects

CLU roots? :slight_smile:

while Ruby does not.

try:

s = “mutable”
hash = { s => “object” }
s.upcase!

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi Bulat,

Do you mean just because we need to story only “len” instead of “len” and
“capa” for each array/tuple? I think it is only a saving of 4 bytes per
array.

Regards,

Bill

···

===========================================================================
Bulat Ziganshin bulatz@integ.ru wrote:

tuple is an array with fixed size. when you have very large number of
small arrays, you will save many memory bytes

Hi,

···

In message “Re: Where Is Method Call Precedence?” on 02/09/30, “Bulat Ziganshin” bulatz@integ.ru writes:

while Ruby does not.

try:

s = “mutable”
hash = { s => “object” }
s.upcase!

Tried?

s = “mutable”
hash = { s => “object” }
s.upcase!
p hash[“mutable”] #=> “object”

						matz.

Hello William,

Monday, September 30, 2002, 5:36:41 PM, you wrote:

no, len is coded in tuple type (Tuple2, Tuple3 and so on). and if you
don’t know, Ruby allocs 16 elems when array is created. i don’t
remember exact conditions of when initial capa will be lesser than 16

···

Do you mean just because we need to story only “len” instead of “len” and
“capa” for each array/tuple? I think it is only a saving of 4 bytes per
array.

tuple is an array with fixed size. when you have very large number of
small arrays, you will save many memory bytes


Best regards,
Bulat mailto:bulatz@integ.ru

Hello William,

Monday, September 30, 2002, 5:36:41 PM, you wrote:

Hi Bulat,

Do you mean just because we need to story only “len” instead of “len” and
“capa” for each array/tuple? I think it is only a saving of 4 bytes per
array.

… but Array_Of_Tuples is better candidate for realization. but in
some cases we need Hash of tuples or some more complex structure

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi Bulat,

Do you mean just because we need to story only “len” instead of “len” and
“capa” for each array/tuple? I think it is only a saving of 4 bytes per
array.

If size is fixed (de)allocation can be made much faster and is
easily managed with a memory arena.

···

On Mon, Sep 30, 2002 at 10:36:41PM +0900, William Djaja Tjokroaminata wrote:

Regards,

Bill

Bulat Ziganshin bulatz@integ.ru wrote:

tuple is an array with fixed size. when you have very large number of
small arrays, you will save many memory bytes


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Debian is like Suse with yast turned off, just better. :slight_smile:
– Goswin Brederlow

Hello Yukihiro,

Monday, September 30, 2002, 10:25:01 AM, you wrote:

s = “mutable”
hash = { s =>> “object” }
s.upcase!
p hash[“mutable”] #=> “object”

p hash[s] #=> nil

:slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi Bulat,

I am using Ruby v 1.6.7 (which is called the “stable version” at
www.ruby-lang.org).

First, I think in array.c “len” is really of type long (4 bytes in my
machine).

Second, if you use “Array.new(n)” (as for tuple also we need to know the
exact size when we create it), then although initially Ruby allocs 16
elements, it will be resized immediately to exactly n elements. So I
guess using tuple will only save memory of 4 bytes per array (assuming the
underlying pointer handle is the same for array and tuple).

Regards,

Bill

···

==========================================================================
Bulat Ziganshin bulatz@integ.ru wrote:

no, len is coded in tuple type (Tuple2, Tuple3 and so on). and if you
don’t know, Ruby allocs 16 elems when array is created. i don’t
remember exact conditions of when initial capa will be lesser than 16