Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class Function call vs Normal Function call

Reply
Thread Tools

Class Function call vs Normal Function call

 
 
THAKUR PRASHANT SINGH
Guest
Posts: n/a
 
      02-26-2010
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 ?

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3DClass =
function=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=
require
'zip/zip'

class ZipUtils
attr :filename
def initialize(filename)
@filename=3Dfilename
end
def createkmzfile
kml_name=3D@filename
puts kml_name
kmz_file =3D kml_name.sub(".kml",".kmz")
=20
File.delete(kmz_file) if File.exists?(kmz_file)
=20
Zip::ZipFile.open(kmz_file, Zip::ZipFile::CREATE) {
|zf|
zf.add(kml_name, "kml/#{kml_name}")
zf.mkdir("images")
imglist=3DDir.glob("images/*.png")
puts imglist=20
for i in 0..imglist.size-1
zf.add(imglist[i], imglist[i])
end =20
}
=20
end
end
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3Dexternal =
file=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= 3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
require 'zip/zip'

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

=20
end
end


kml_name=3D"LOC_0.kml"
kmz_file =3D kml_name.sub(".kml",".kmz")
puts kml_name
File.delete(kmz_file) if File.exists?(kmz_file)
=20
Zip::ZipFile.open(kmz_file, Zip::ZipFile::CREATE) {
|zf|
zf.add(kml_name, "kml/#{kml_name}")
zf.mkdir("images")
imglist=3DDir.glob("images/*.png")
puts imglist=20
for i in 0..imglist.size-1
zf.add(imglist[i], imglist[i])
end
}




 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      02-26-2010
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
THAKUR PRASHANT SINGH
Guest
Posts: n/a
 
      02-26-2010
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=3D ZipUtils.new(@file_names[file_index])
zip_file.createkmzfile

-----Original Message-----
From: [private.php?do=newpm&u=]=20
Sent: Friday, February 26, 2010 2:24 PM
To: ruby-talk ML
Subject: Re: Class Function call vs Normal Function call

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

Please make two complete standalone test programs to demonstrate your=20
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=20
problems where the issue lies.

It's also useful to know what version of ruby you're running under, and=20
what O/S.
--=20
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      02-26-2010
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)

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

 
Reply With Quote
 
THAKUR PRASHANT SINGH
Guest
Posts: n/a
 
      02-26-2010
Hi Brain,
I have tried to create a test application with code like

zip_file=3D 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

-----Original Message-----
From: [private.php?do=newpm&u=]=20
Sent: Friday, February 26, 2010 4:36 PM
To: ruby-talk ML
Subject: Re: Class Function call vs Normal Function call

THAKUR PRASHANT SINGH wrote:
> The first piece of code is called as lines below from another class
> zip_file=3D ZipUtils.new(@file_names[file_index])
> zip_file.createkmzfile


That's no good. As I said, you need to create two separate *standalone*=20
test cases which replicate the problem.

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

Hence the problem lies somewhere outside, and so it's up to you to=20
provide two *complete* runnable programs which demonstrate the problem.

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

--=20
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
THAKUR PRASHANT SINGH
Guest
Posts: n/a
 
      02-26-2010
Thanks for help I think I got the issue solved.
file =3D 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

-----Original Message-----
From: THAKUR PRASHANT SINGH=20
Sent: Friday, February 26, 2010 4:48 PM
To: ruby-talk ML
Subject: Re: Class Function call vs Normal Function call

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

zip_file=3D 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

-----Original Message-----
From: [private.php?do=newpm&u=]=20
Sent: Friday, February 26, 2010 4:36 PM
To: ruby-talk ML
Subject: Re: Class Function call vs Normal Function call

THAKUR PRASHANT SINGH wrote:
> The first piece of code is called as lines below from another class
> zip_file=3D ZipUtils.new(@file_names[file_index])
> zip_file.createkmzfile


That's no good. As I said, you need to create two separate *standalone*=20
test cases which replicate the problem.

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

Hence the problem lies somewhere outside, and so it's up to you to=20
provide two *complete* runnable programs which demonstrate the problem.

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

--=20
Posted via http://www.ruby-forum.com/.



 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      02-26-2010
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
THAKUR PRASHANT SINGH
Guest
Posts: n/a
 
      02-27-2010
I will do the changes proposed. It indeed looks a good solution
Thanks,
Prashant

-----Original Message-----
From: [private.php?do=newpm&u=]=20
Sent: Saturday, February 27, 2010 2:09 AM
To: ruby-talk ML
Subject: Re: Class Function call vs Normal Function call

THAKUR PRASHANT SINGH wrote:
> Thanks for help I think I got the issue solved.
> file =3D 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=20
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=20
exception is raised in the block.
--=20
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Partial Function Application -- Advantages over normal function? Kurian Thayil Python 3 07-18-2011 08:17 PM
why inline is faster than normal function call? thomas C++ 2 11-13-2009 04:47 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
What's the difference between built-in func getattr() and normal call of a func of a class Johnny Python 3 08-23-2005 08:28 AM
Cannot covert non-static member function to normal function? Morgan Cheng C++ 4 12-17-2004 09:44 AM



Advertisments
 



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