[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

a = b = c order of evaluation weird

SpringFlowers AutumnMoon

10/2/2007 9:55:00 AM

i thought if it is

a = b = c

due to associativity rule, then it is

a = (b = c)

so (b = c) is evaluated first. and then now it will be a =
(evaluated_value)

now how come when

a = Array(1..100)

and to cut off the first 1/3 and last 1/3 of the array to get about 33
elements, shouldn't we use

a[0..a.size/2] = a[a.size*2/3..-1] = nil

as after the last 1/3 is deleted, you got about 66 elements remaining
and we want the other half deleted, to get to 33 elements. However, it
won't work and requires

a[0..a.size/3] = a[a.size*2/3..-1] = nil

why is that?
--
Posted via http://www.ruby-....

23 Answers

7stud 7stud

10/2/2007 10:13:00 AM

0

SpringFlowers AutumnMoon wrote:
>
> why is that?
>

a = Array(1..6)
p a

puts "size: #{a.size/3}"
a[p(0..a.size/3)] = a[p(a.size*2/3..-1)] = nil



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

7stud 7stud

10/2/2007 10:16:00 AM

0

7stud -- wrote:
> SpringFlowers AutumnMoon wrote:
>>
>> why is that?

Or even simpler:

a = Array(1..6)

a[puts "hello"] = a[puts "world"] = nil
--
Posted via http://www.ruby-....

David A. Black

10/2/2007 10:18:00 AM

0

David A. Black

10/2/2007 10:21:00 AM

0

David A. Black

10/2/2007 10:22:00 AM

0

Chris Bailey

10/2/2007 10:25:00 AM

0

I'm having a bit of a problem accessing variables in an instance of GServer.
What I would like to do is make a hash that is effectively global to the
current thread.

-/Server.rb
require 'gserver'
require 'connect.rb'

class TestServer < GServer
def initialize(port = 4000, *args)
super(port, *args)
end
def serve( io )
@test_hash = Hash.new
connect(io) #Defined in connect.rb
loop do
str = io.gets
parser(str,io)
end
end
end

The way that I'm doing it doesn't allow each thread to have it's own copy of
@test_hash and that's my problem. I need each thread to be able to change
the data stored in the hash without affecting the data stored in the hash
for all threads. I'm sure that my understanding of scope and GServer itself
is causing my problem, but I just don't know what to do to fix it. I was
thinking that I could pass the hash as a parameter to the connect function
but that would quickly become a problem as it would need to be passed to
several other functions after that and if possible I just don't want to have
to use that many extra parameters in my functions. Any help would be
appreciated.



Bertram Scharpf

10/2/2007 11:03:00 AM

0

Hi,

Am Dienstag, 02. Okt 2007, 19:25:07 +0900 schrieb Chris Bailey:
> I'm having a bit of a problem [...]

Please have a look where your message appears when you just
hit the "Reply" button:

<http://news.gmane.org/gmane.comp.lang.ruby.general/cutoff=...

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

7stud 7stud

10/2/2007 11:31:00 AM

0

David A. Black wrote:
> On Tue, 2 Oct 2007, 7stud -- wrote:
>
>> 7stud -- wrote:
>>> SpringFlowers AutumnMoon wrote:
>>>>
>>>> why is that?
>>
>> Or even simpler:
>>
>> a = Array(1..6)
>>
>> a[puts "hello"] = a[puts "world"] = nil
>
> puts returns nil, and you can't index an array with nil.
>
>

Yes, I know, but that isn't the point of the example. The output
provides the answer to the question.
--
Posted via http://www.ruby-....

7stud 7stud

10/2/2007 11:52:00 AM

0

7stud -- wrote:
>>>
>>> a[puts "hello"] = a[puts "world"] = nil
>>
>> puts returns nil, and you can't index an array with nil.
>>

How's this:

a = Array(1..6)

a[(puts "hello", 0..a.size/2;0..a.size/2)] = a[(puts "world",
a.size*2/3..1;a.size*2/3..1)] = nil
p a


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

Calamitas

10/2/2007 12:07:00 PM

0

On 02/10/2007, 7stud -- <dolgun@excite.com> wrote:
> David A. Black wrote:
> > On Tue, 2 Oct 2007, 7stud -- wrote:
> >
> >> 7stud -- wrote:
> >>> SpringFlowers AutumnMoon wrote:
> >>>>
> >>>> why is that?
> >>
> >> Or even simpler:
> >>
> >> a = Array(1..6)
> >>
> >> a[puts "hello"] = a[puts "world"] = nil
> >
> > puts returns nil, and you can't index an array with nil.
> >
> >
>
> Yes, I know, but that isn't the point of the example. The output
> provides the answer to the question.

My impression was that the OP knew what the evaluation order is, but
not why it is like that.

But anyway, my take on this is that

a[0..a.size/2] = a[a.size*2/3..-1] = nil

is equivalent to:

a.[]=(0..a.size/2, a.[]=(a.size*2/3..-1, nil))

Arguments are evaluated left to right, completely explaining the
evaluation order the OP is seeing.

Peter