# Class Function call vs Normal Function call

**URL:** <https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181>\
**Category:** Ruby\
**Created:** [February 26, 2010, 8:17am UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181 "2010-02-26T08:17:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 8:17am UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/1 "2010-02-26T08:17:47Z")

</div>

I am creating zip file using ruby zip there are two versions of creating  
the zip file 1st version part of class and in 2nd version running it  
alone.  
The kmz file created with 1st version doesn’t contain complete text of  
LOC\_0.kml Whereas the 2nd version does contain the complete file  
contents…  
I.e. if LOC\_0.kml has 100 lines then 1st version kmz file contains only  
95 lines while 2nd version contains complete 100 lines.  
Can you please suggest what can be the reason ?

====================Class function====================== require  
‘zip/zip’

class ZipUtils  
attr :filename  
def initialize(filename)  
@filename=filename  
end  
def createkmzfile  
kml\_name=@filename  
puts kml\_name  
kmz\_file = kml\_name.sub(".kml",".kmz")

```
File.delete(kmz_file) if File.exists?(kmz_file)

Zip::ZipFile.open(kmz_file, Zip::ZipFile::CREATE) {
  |zf|
  zf.add(kml_name, "kml/#{kml_name}")
  zf.mkdir("images")
  imglist=Dir.glob("images/*.png")
  puts imglist
  for i in 0..imglist.size-1
    zf.add(imglist[i], imglist[i])
  end
}

```

end  
end  
==========================external file===========================  
require ‘zip/zip’

class ZipUtils  
attr :filename  
def initialize(filename)  
@filename=filename  
end  
def createkmzfile

end  
end

```
kml_name="LOC_0.kml"
kmz_file = kml_name.sub(".kml",".kmz")
   puts kml_name
File.delete(kmz_file) if File.exists?(kmz_file)

Zip::ZipFile.open(kmz_file, Zip::ZipFile::CREATE) {
  |zf|
  zf.add(kml_name, "kml/#{kml_name}")
  zf.mkdir("images")
  imglist=Dir.glob("images/*.png")
  puts imglist
  for i in 0..imglist.size-1
    zf.add(imglist[i], imglist[i])
  end
}
```

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 11:55am UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/2 "2010-02-26T11:55:31Z")

</div>

The ruby version is ruby 1.8.6 (2008-08-11 patchlevel 287)  
[i386-mswin32]  
The OS is Win XP  
The first piece of code is called as lines below from another class  
zip\_file= ZipUtils.new(@file\_names[file\_index])  
zip\_file.createkmzfile

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 9:53am UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/3 "2010-02-26T09:53:53Z")

</div>

Your first piece of code doesn’t do anything at all, because it just  
creates a class ZipUtils but does nothing with it (never instantiates  
it, never calls a class method).

Please make two complete standalone test programs to demonstrate your  
problem. This means that (a) we can see if we can duplicate your problem  
on our own machines, and (b) we can tell you why it’s not working as you  
expect. Or you may find in the process of making these standalone  
problems where the issue lies.

It’s also useful to know what version of ruby you’re running under, and  
what O/S.

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 12:06pm UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/4 "2010-02-26T12:06:23Z")

</div>

THAKUR PRASHANT SINGH wrote:

> The first piece of code is called as lines below from another class  
> zip\_file= ZipUtils.new(@file\_names[file\_index])  
> zip\_file.createkmzfile

That’s no good. As I said, you need to create two separate _standalone_  
test cases which replicate the problem.

This is because the problem you describe makes no sense just with the  
code snippets you posted, since the code is the same in both cases, as  
you know.

Hence the problem lies somewhere outside, and so it’s up to you to  
provide two _complete_ runnable programs which demonstrate the problem.

Since these programs read external data, you’ll need to provide some  
sample data files too (or better, include some short data items within  
the code itself, if you can still replicate the problem like this)

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 12:18pm UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/5 "2010-02-26T12:18:20Z")

</div>

Hi Brain,  
I have tried to create a test application with code like

```
 zip_file= ZipUtils.new('FILE_0.kml')
  zip_file.createkmzfile

```

this works fine.  
The problem comes which I think is when we have very high CPU usage in  
my case it was 50% i.e. in dual processor machine a processor was  
completely occupied.  
Can this be a reason ?  
The test scenario can be load the CPU in program and then execute these  
lines when utilization reaches these levels.  
I got a similar issue when I was using REXML which got corrected when I  
switched to Nokogiri…  
Any other pointers ??  
Regards,  
Prashant

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 9:39pm UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/6 "2010-02-26T21:39:08Z")

</div>

THAKUR PRASHANT SINGH wrote:

> Thanks for help I think I got the issue solved.  
> file = File.open(“kml/#{@file\_names[file\_index]}”,“w”)  
> file.write(@kml\_file\_data.to\_xml)  
> file.close  
> The last line was missing. When I closed the file the problem  
> disappeared

And this problematic code wasn’t in the source you posted 🙂

For a cleaner solution, you could use the same block form that you were  
using for the zip files:

File.open(“kml/#{@file\_names[file\_index]}”,“w”) do |file|  
file.write(@kml\_file\_data.to\_xml)  
end

This is better because the file will _always_ be closed, even if an  
exception is raised in the block.

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 26, 2010, 12:25pm UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/7 "2010-02-26T12:25:22Z")

</div>

Thanks for help I think I got the issue solved.  
file = File.open(“kml/#{@file\_names[file\_index]}”,“w”)  
file.write(@kml\_file\_data.to\_xml)  
file.close  
The last line was missing. When I closed the file the problem  
disappeared

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecc23a/32.png) [@THAKUR\_PRASHANT\_SIN](https://www.ruby-forum.com/u/THAKUR_PRASHANT_SIN)\
**Post date:** [February 27, 2010, 8:04am UTC](https://www.ruby-forum.com/t/class-function-call-vs-normal-function-call/184181/8 "2010-02-27T08:04:41Z")

</div>

I will do the changes proposed. It indeed looks a good solution  
Thanks,  
Prashant
