[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Interesting Array Initialization Typo

Maciej Tomaka

7/25/2008 2:59:00 PM

When initializing for example : [ [1, 2], [2, 3], [3, 2] ]

You may forget about one colon, you will get:

>> [ [1, 2] [2, 3], [3, 2] ]
=> [[], [3, 2]]

No syntax error:) No warnings here, beware! :)
--
Posted via http://www.ruby-....

12 Answers

ara.t.howard

7/25/2008 3:14:00 PM

0


On Jul 25, 2008, at 8:58 AM, Maciej Tomaka wrote:

> When initializing for example : [ [1, 2], [2, 3], [3, 2] ]
>
> You may forget about one colon, you will get:
>
>>> [ [1, 2] [2, 3], [3, 2] ]
> => [[], [3, 2]]
>
> No syntax error:) No warnings here, beware! :)
> --
> Posted via http://www.ruby-....
>

it's not a syntax error, it's the same as this

cfp:~ > ruby -e' p( ( array = [1,2] )[ index = [2,3] ] ) '


which is the same as

cfp:~ > ruby -e' array = [1,2]; index = [2,3]; p array[*index]'
[]


[1, 2] [2, 3] is the array '[1,2]' indexed by '2,3'


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Maciej Tomaka

7/25/2008 3:30:00 PM

0

Ara Howard wrote:
> On Jul 25, 2008, at 8:58 AM, Maciej Tomaka wrote:
>
>>
> it's not a syntax error, it's the same as this
>
> cfp:~ > ruby -e' p( ( array = [1,2] )[ index = [2,3] ] ) '
>
>
> which is the same as
>
> cfp:~ > ruby -e' array = [1,2]; index = [2,3]; p array[*index]'
> []
>
>
> [1, 2] [2, 3] is the array '[1,2]' indexed by '2,3'
>
>
> a @ http://codeforp...

I undestand what is going on here.

It just might be tricky to figure what's wrong in code with such typo.


--
Posted via http://www.ruby-....

Shadowfirebird

7/25/2008 4:18:00 PM

0

For less trivial cases I tend to:

a = []
a << [1,2]
a << [2,3]
a << [3,4]

Absolutely no chance of misreading that.

Dave Bass

7/27/2008 10:22:00 AM

0

Regarding typos: Ruby can't be expected to know what you meant to type,
so if it's syntactically correct it will let it through -- as with any
language! To do otherwise would be as annoying as Microsoft's Clippy: "I
see you're trying to set up an array. Did you just make a typo? <wink>"

Shadowfirebird wrote:
> a = []
> a << [1,2]
> a << [2,3]
> a << [3,4]

Without any actual understanding of what's going on inside Ruby
(sorry!), I suspect this code will be slower, as the array has to be
grown three times, rather than springing into existence fully
initialised. Or is this a negligible overhead? Or does the code get
optimised by the compiler/interpreter? Maybe someone knowledgeable would
like to comment.

In particular it would be interesting to know if putting array pushes,
pops, shifts and unshifts inside a big loop is inefficient.

Dave
--
Posted via http://www.ruby-....

Justin Collins

7/29/2008 7:47:00 PM

0

Dave Bass wrote:
> Regarding typos: Ruby can't be expected to know what you meant to type,
> so if it's syntactically correct it will let it through -- as with any
> language! To do otherwise would be as annoying as Microsoft's Clippy: "I
> see you're trying to set up an array. Did you just make a typo? <wink>"
>
> Shadowfirebird wrote:
>
>> a = []
>> a << [1,2]
>> a << [2,3]
>> a << [3,4]
>>
>
> Without any actual understanding of what's going on inside Ruby
> (sorry!), I suspect this code will be slower, as the array has to be
> grown three times, rather than springing into existence fully
> initialised. Or is this a negligible overhead? Or does the code get
> optimised by the compiler/interpreter? Maybe someone knowledgeable would
> like to comment.
>
> In particular it would be interesting to know if putting array pushes,
> pops, shifts and unshifts inside a big loop is inefficient.
>
> Dave
>

Here's what I got:

Rehearsal --------------------------------------
<< 0.990000 0.290000 1.280000 ( 1.331386)
[] 0.060000 0.000000 0.060000 ( 0.070595)
----------------------------- total: 1.340000sec

user system total real
<< 0.940000 0.320000 1.260000 ( 1.302148)
[] 0.060000 0.010000 0.070000 ( 0.070150)


With
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux-gnu]

and

require 'benchmark'
include Benchmark

bmbm do |t|
t.report("<<") do
10000.times do
a = []
100.times do |x|
a << x
end
end
end

t.report("[]") do
10000.times do
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
end
end
end


-Justin

F. Senault

7/30/2008 8:03:00 AM

0

Le 29 juillet à 21:46, Justin Collins a écrit :

> Here's what I got:
>
> Rehearsal --------------------------------------
> << 0.990000 0.290000 1.280000 ( 1.331386)
> [] 0.060000 0.000000 0.060000 ( 0.070595)
> ----------------------------- total: 1.340000sec
>
> user system total real
> << 0.940000 0.320000 1.260000 ( 1.302148)
> [] 0.060000 0.010000 0.070000 ( 0.070150)

Note that I tried to add this to your test :

t.report("=") do
10000.times do
a = Array.new(100)
100.times do |x|
a[x] = x
end
end
end

And...

Rehearsal --------------------------------------
<< 0.304688 0.007812 0.312500 ( 0.313559)
= 0.304688 0.000000 0.304688 ( 0.306314)
[] 0.031250 0.000000 0.031250 ( 0.027802)
----------------------------- total: 0.648438sec

user system total real
<< 0.304688 0.000000 0.304688 ( 0.309506)
= 0.296875 0.000000 0.296875 ( 0.299347)
[] 0.023438 0.007812 0.031250 ( 0.028271)

Surprising !

(ruby 1.8.6 (2007-09-24 patchlevel 111) [amd64-freebsd7])

Fred
--
The Admins are the priesthood of an irrational, anarchistic, random,
pseudo-religious hardware platform, trying to impose their will on
people who would rather be using us to avoid real work.
(Joe Moore in the SDM)

Maciej Tomaka

7/30/2008 9:25:00 AM

0

The correct benchmark is:
(look how long x.times takes :)
require 'benchmark'
include Benchmark

bmbm do |t|
t.report("<<") do
100_000.times do
a = []
a << [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a << [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
a << [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
a << [ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
a << [ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
a << [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
a << [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
a << [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
a << [ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
a << [ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
end
end

t.report("[]") do
100_000.times do
a = [
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
]
end
end

t.report("NO") do
100_000.times do
100.times do |x|
end
end
end
end

Rehearsal --------------------------------------
<< 1.020000 0.020000 1.040000 ( 1.047729)
[] 0.730000 0.010000 0.740000 ( 0.741970)
NO 4.270000 1.510000 5.780000 ( 5.808074)
----------------------------- total: 7.560000sec

user system total real
<< 1.000000 0.020000 1.020000 ( 1.032165)
[] 0.740000 0.010000 0.750000 ( 0.743470)
NO 5.070000 1.560000 6.630000 ( 6.634026)

So << and [] are comparable.

Shadowfirebird has good solution inmho w/o significant perfomance
impact.
--
Posted via http://www.ruby-....

Maciej Tomaka

7/30/2008 9:43:00 AM

0

tested again on 1.8.6,
previous was on 1.8.4 (2005-12-24) [i486-linux] 1.8.4)

Rehearsal --------------------------------------
<< 0.742188 0.000000 0.742188 ( 0.748359)
[] 0.570312 0.000000 0.570312 ( 0.567802)
NO 0.007812 0.000000 0.007812 ( 0.009996)
----------------------------- total: 1.320312sec

user system total real
<< 0.742188 0.000000 0.742188 ( 0.747654)
[] 0.570312 0.000000 0.570312 ( 0.567690)
NO 0.007812 0.000000 0.007812 ( 0.009741)


ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]
--
Posted via http://www.ruby-....

Maciej Tomaka

7/30/2008 9:51:00 AM

0

Maciej Tomaka wrote:
> tested again on 1.8.6,
> previous was on 1.8.4 (2005-12-24) [i486-linux] 1.8.4)
>
> Rehearsal --------------------------------------
> << 0.742188 0.000000 0.742188 ( 0.748359)
> [] 0.570312 0.000000 0.570312 ( 0.567802)
> NO 0.007812 0.000000 0.007812 ( 0.009996)
> ----------------------------- total: 1.320312sec
>
> user system total real
> << 0.742188 0.000000 0.742188 ( 0.747654)
> [] 0.570312 0.000000 0.570312 ( 0.567690)
> NO 0.007812 0.000000 0.007812 ( 0.009741)
>
>
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

The 'NO' test was wrong.
This is retest with code that was pasted before:

Rehearsal --------------------------------------
<< 0.750000 0.000000 0.750000 ( 0.749043)
[] 0.570312 0.000000 0.570312 ( 0.571285)
NO 1.054688 0.000000 1.054688 ( 1.056768)
----------------------------- total: 2.375000sec

user system total real
<< 0.750000 0.000000 0.750000 ( 0.745733)
[] 0.562500 0.000000 0.562500 ( 0.567207)
NO 1.039062 0.000000 1.039062 ( 1.040605)
--
Posted via http://www.ruby-....

F. Senault

7/30/2008 9:51:00 AM

0

Le 30 juillet à 11:24, Maciej Tomaka a écrit :

> The correct benchmark is:

/.../

In both cases, your arrays are nested ; you're not testing the same
thing...

Fred
--
As far as the laws of mathematics refer to reality, they are not
certain; and as far as they are certain, they do not refer to reality.
(Albert Einstein)