4 Replies - 2165 Views - Last Post: 16 December 2011 - 01:12 PM Rate Topic: -----

#1 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Trouble creating a new file in ruby

Posted 16 December 2011 - 06:48 AM

I want to create a script merging all files of a special type.
But I am having troubles creating a mergeFile.
My code looks like this:
text = ''
Dir[File.join(".", "**/*.java")].each do |file|
	File.open(file).each do |line|
		text << line + "\n" 
	end
	text << "\n"
end

File.new("/Users/tools/Documents/workspace_2/Project_Carletti/merge.java")
File.open("merge.java", 'w'){
	|f| f.write(text)
}




I can't find my error, can anyone tell me what is wrong

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble creating a new file in ruby

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Trouble creating a new file in ruby

Posted 16 December 2011 - 07:19 AM

Dir[File.join(".", "**/*.java")]


No reason that shouldn't just be Dir["**/*.java"].

File.open(file).each do |line|


That should be File.foreach(file) do |line|. The way you wrote it, you're leaking the file handle (until it gets collected anyway). Unlike open(...).each, foreach will close the file automatically when it's done.

In general when you use File.open without a block, you're probably doing something wrong.

text << line + "\n"


The string yielded by each already has a linebreak at the end. So your code will insert an empty line after each line.

Also it might be smarter to write each line into the target file directly rather than using a temporary string that you're appending to. Otherwise you're going to be using a lot of memory when you merge a lot of big files.

File.new("/Users/tools/Documents/workspace_2/Project_Carletti/merge.java")


Not sure what you trying to do here, but since you never use the File object being created here, that line is completely superfluous. It will also blow up if the file does not exist and if /Users/tools/Documents/workspace_2/Project_Carletti/merge.java is the same "merge.java" that you're trying to open for writing in the next line, it might prevent you from doing so as some operating systems prevent you from opening the same file more than once at a time.

File.open("merge.java", 'w')


Assuming this is indeed the same file as on the line above, I don't know why you used the full path before and only the relative path now. The above code will create a file called "merge.java" in the directory that you invoke your script from. If you want to create it somewhere else, you'll need to specify the whole path.

This post has been edited by sepp2k: 16 December 2011 - 07:20 AM

Was This Post Helpful? 2
  • +
  • -

#3 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: Trouble creating a new file in ruby

Posted 16 December 2011 - 11:17 AM

What I want to do is creating a new file called merge.xx
In the folder where the script is run from
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Trouble creating a new file in ruby

Posted 16 December 2011 - 11:58 AM

Good then File.open("merge.java", 'w') is fine (well if you really want the extension to be .xx, you'll need to change it to "merge.xx" of course).

That still begs the question why you used an absolute path in the line above and what purpose that line was supposed to serve. My guess is you'll be fine if you simply remove the File.new line.
Was This Post Helpful? 0
  • +
  • -

#5 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: Trouble creating a new file in ruby

Posted 16 December 2011 - 01:12 PM

with xx I mean random extension.

But thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1