[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Globals not incrementing inside block

Todd A. Jacobs

6/7/2007 1:34:00 AM

I have the following snippet:

at_exit do
$total_count, $daily_count = 0, 0
IO.foreach(my_logfile) do |line|
$total_count.next
$daily_count.next if line =~ /^#{Time.now.strftime('%Y%m%d')}/
end
printf("\ns Total: %d\ns Today:%d\n", $total_count,
$daily_count)
end

I assumed I had to use globals, since otherwise the block would treat
the counters as local variables, but I have the same problem either way:
the counters never increase. They still both read zero at the end of the
script, even though some strategic puts statements show that the file is
successfully being read.

What newbie mistake am I making now?

--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"

10 Answers

Ezra Zygmuntowicz

6/7/2007 1:48:00 AM

0


On Jun 6, 2007, at 6:34 PM, Todd A. Jacobs wrote:

> I have the following snippet:
>
> at_exit do
> $total_count, $daily_count = 0, 0
> IO.foreach(my_logfile) do |line|
> $total_count.next
> $daily_count.next if line =~ /^#{Time.now.strftime('%Y%m%d')}/
> end
> printf("\ns Total: %d\ns Today:%d\n", $total_count,
> $daily_count)
> end
>
> I assumed I had to use globals, since otherwise the block would treat
> the counters as local variables, but I have the same problem either
> way:
> the counters never increase. They still both read zero at the end
> of the
> script, even though some strategic puts statements show that the
> file is
> successfully being read.
>
> What newbie mistake am I making now?
>
> --
> "Oh, look: rocks!"
> -- Doctor Who, "Destiny of the Daleks"
>


$total_count.next does not increment $total_count try this instead:

$total_count += 1

Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)



Zachary Holt

6/7/2007 1:52:00 AM

0

Hi,

On Jun 6, 2007, at 6:34 PM, Todd A. Jacobs wrote:

> I have the following snippet:
>
> at_exit do
> $total_count, $daily_count = 0, 0
> IO.foreach(my_logfile) do |line|
> $total_count.next

You're not storing the new value
$total_count = $total_count.next
or
$total_count += 1

Also, you don't need globals to do what you're trying to do.


Harry Kakueki

6/7/2007 2:05:00 AM

0

On 6/7/07, Todd A. Jacobs <tjacobs-sndr-019fdb@codegnome.org> wrote:
>
> I assumed I had to use globals, since otherwise the block would treat
> the counters as local variables, but I have the same problem either way:
> the counters never increase. They still both read zero at the end of the
> script, even though some strategic puts statements show that the file is
> successfully being read.
>
> What newbie mistake am I making now?
>
> --
> "Oh, look: rocks!"
> -- Doctor Who, "Destiny of the Daleks"
>
>
You don't need global variables.

total = 0
5.times do
total += 1
end

p total # 5

Harry


--

A Look into Japanese Ruby List in English
http://www.ka...

Rubén Medellín

6/7/2007 2:32:00 AM

0


> You don't need global variables.
>
> total = 0
> 5.times do
> total += 1
> end

Unless you want your snippet to have side effects. Which is unlikely,
since the method is called at_exit. Anyway, you are assigning the
variables new values, so you can use local ones instead.


__________________________________________

Dictionary is the only place that success comes before work. - Vince
Lombardi

Todd A. Jacobs

6/7/2007 6:13:00 PM

0

On Thu, Jun 07, 2007 at 01:55:23PM +0900, Ivan Salazar wrote:

> This one is a very common mistake when someone is new to functional
> languages specially when having some procedural language experience...
> Many methods in functional languages don't have side effects (don't

Thanks. In retrospect, this is somewhat obvious. But you're right: it
was my expectation that foo.succ was equivalent to foo+=1, rather than
simply being an expression that returned a value without modifying the
variable itself.

I appreciate all of the informative responses.

--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"

Lloyd Linklater

6/7/2007 7:10:00 PM

0

I guess this means that you will be going out with a bang! :)

(sorry. I could not resist!)

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

Rick DeNatale

6/7/2007 7:15:00 PM

0

On 6/7/07, Todd A. Jacobs <tjacobs-sndr-019fdb@codegnome.org> wrote:
> On Thu, Jun 07, 2007 at 01:55:23PM +0900, Ivan Salazar wrote:
>
> > This one is a very common mistake when someone is new to functional
> > languages specially when having some procedural language experience...
> > Many methods in functional languages don't have side effects (don't
>
> Thanks. In retrospect, this is somewhat obvious. But you're right: it
> was my expectation that foo.succ was equivalent to foo+=1, rather than
> simply being an expression that returned a value without modifying the
> variable itself.

May I have the temerity to point out that there's another subtle
misconception in this statement which I see a lot of Ruby nubies trip
up on, and that's thinking that variables are objects.

An object holds state and behavior, a variable references an object.
More than one variable can reference the same object.

Modifying an object means modifying the objects state.

Modifying a variable means changing WHICH object it is referencing.
Which is done by assignment or assignment-like things such as
providing a value to the parameter of a method or block.

I would argue that: a = a.chop! doesn't change the variable a since
it's still referring to the same object. Consider:

a = "abc"
# changes (sets) VARIABLE a

b = a
# changes (sets) VARIABLE b

p a.object_id => -606250168
p b.object_id => -606250168
# note that its one OBJECT referenced by two VARIABLES

c = a.delete('a') => "bc"
# creates a new OBJECT leaving both VARIABLEs
# and the OBJECT they reference unchanged.
p a => "abc"
p b => "abc"
p a.object_id => -606250168
p b.object_id => -606250168

b.delete!('b')
# changes the state of the OBJECT referenced by VARIABLE b
p b => "ac"

p b.object_id => -606250168
# but leaves the VARIABLE b unchanged, it's still referencing the same
# object albeit that object's state has changed.
p a => "ac"
p a.object_id => -606250168
# and VARIABLE a still refers to the same (changed) OBJECT

b = c
# changes the VARIABLE b
p b.object_id => -606269188
p c.object_id => -606269188


And variable is a general term covering (local|global|instance|class)
variables as well as things like the slots in Arrays which refer to
the elements of the array.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

ivan.salazarv

6/14/2007 5:25:00 AM

0



On Jun 7, 2:15 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:
> On 6/7/07, Todd A. Jacobs <tjacobs-sndr-019...@codegnome.org> wrote:
>
> > On Thu, Jun 07, 2007 at 01:55:23PM +0900, Ivan Salazar wrote:
>
> > > This one is a very common mistake when someone is new to functional
> > > languages specially when having some procedural language experience...
> > > Many methods in functional languages don't have side effects (don't
>
> > Thanks. In retrospect, this is somewhat obvious. But you're right: it
> > was my expectation that foo.succ was equivalent to foo+=1, rather than
> > simply being an expression that returned a value without modifying the
> > variable itself.
>
> May I have the temerity to point out that there's another subtle
> misconception in this statement which I see a lot of Ruby nubies trip
> up on, and that's thinking that variables are objects.
>
> An object holds state and behavior, a variable references an object.
> More than one variable can reference the same object.
>
> Modifying an object means modifying the objects state.
>
> Modifying a variable means changing WHICH object it is referencing.
> Which is done by assignment or assignment-like things such as
> providing a value to the parameter of a method or block.
>
> I would argue that: a = a.chop! doesn't change the variable a since
> it's still referring to the same object. Consider:
>
> a = "abc"
> # changes (sets) VARIABLE a
>
> b = a
> # changes (sets) VARIABLE b
>
> p a.object_id => -606250168
> p b.object_id => -606250168
> # note that its one OBJECT referenced by two VARIABLES
>
> c = a.delete('a') => "bc"
> # creates a new OBJECT leaving both VARIABLEs
> # and the OBJECT they reference unchanged.
> p a => "abc"
> p b => "abc"
> p a.object_id => -606250168
> p b.object_id => -606250168
>
> b.delete!('b')
> # changes the state of the OBJECT referenced by VARIABLE b
> p b => "ac"
>
> p b.object_id => -606250168
> # but leaves the VARIABLE b unchanged, it's still referencing the same
> # object albeit that object's state has changed.
> p a => "ac"
> p a.object_id => -606250168
> # and VARIABLE a still refers to the same (changed) OBJECT
>
> b = c
> # changes the VARIABLE b
> p b.object_id => -606269188
> p c.object_id => -606269188
>
> And variable is a general term covering (local|global|instance|class)
> variables as well as things like the slots in Arrays which refer to
> the elements of the array.
>
> --
> Rick DeNatale
>
> My blog on Rubyhttp://talklikeaduck.denh...


I agree with you, that is also a frequent misconception.

>In brief, to modify an object you make a call to a method with
>side effects or reassign the object to the new one created after the method
>call.

The one above should read: "or reassign the variable to the new object
created".
(I don't know what happened, maybe I got confused while writing or it
was just
my good-old stupidity XD)


Rick DeNatale

6/14/2007 4:22:00 PM

0

On 6/14/07, Ivan Salazar <ivan.salazarv@gmail.com> wrote:
> On Jun 7, 2:15 pm, "Rick DeNatale" <rick.denat...@gmail.com> wrote:

> > May I have the temerity to point out that there's another subtle
> > misconception in this statement which I see a lot of Ruby nubies trip
> > up on, and that's thinking that variables are objects.
> >
> > An object holds state and behavior, a variable references an object.
> I agree with you, that is also a frequent misconception.
>
> >In brief, to modify an object you make a call to a method with
> >side effects or reassign the object to the new one created after the method
> >call.
>
> The one above should read: "or reassign the variable to the new object
> created".
> (I don't know what happened, maybe I got confused while writing or it
> was just
> my good-old stupidity XD)

Just nit-picking but even with that change, it's still not quite
right, reassigning a variable doesn't modify the object it previously
referenced.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Rick DeNatale

6/23/2007 8:25:00 PM

0

On 6/23/07, Ivan Salazar <ivan.salazarv@gmail.com> wrote:
> Ohhh, right. I know where is the mistake... My poor english. Im still not
> writing what I want to say XD.
> My bad, I am very sorry. But that's what I ment to say.

Nothing to be sorry about Ivan.

--
Rick