0

Ok, I tried to delete lines that contain -- from a file. The file looks like this:

  -----------------------------------------------------------------
  Lorem ipsum....
  Text.....

And my code:

  f = File.open('C:\websites\ahr.txt')
  f.each_line do |line|
    if line.include? '-'
     a = line
     line.delete a
    end
  end
f = File.close

But it won't work! I get this error:

 file.rb:5:in `delete': invalid range "-- (ArgumentError)
 " in string transliteration
    from file.rb:5:in `block in <main>'
    from file.rb:2:in `each_line'
    from file.rb:2:in `<main>'

What's going on?

Rafał Cieślak
  • 972
  • 1
  • 8
  • 25
Em Sta
  • 1,676
  • 9
  • 29
  • 43

2 Answers2

2

So whats going on?

Something like this:

line = "-----------------------------------\n"
 => "-----------------------------------\n"

a = line
 => "-----------------------------------\n"

line.delete a
 ArgumentError: invalid range "--
 " in string transliteration

The error raised by the delete method is because dashes are used for special character-range syntax:

"hellob".delete "a-e"
 => "hllo"

Probably you want to be taking a different approach to solve your goal, but it is difficult to tell what you want, as you don't show what you want to do with the data once lines are removed. Simply processing the line variable just means you have an altered String at that point.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
  • But what happens If i dont now how long the line with the –––is? – Em Sta Jul 25 '13 at 10:14
  • @Em Sta: Nothing happens either way in your script as-is. You have the behaviour of `delete` wrong. As written. it wont even change the content of `line` variable, let alone the content of the file. It *returns* an altered string, which your code is not using in any way. – Neil Slater Jul 25 '13 at 10:17
  • sorry that i responded so late. My aim is that the lines that contain - or several - are deleted. But i dont want that the line is blank! – Em Sta Jul 25 '13 at 10:30
  • 1
    @Em Sta: In Ruby you will need to solve this with code that writes strings that you want to a file opened for writing (typically using syntax like `file.puts line`). There is no syntax that modifies or deletes lines of a file in place. On the plus side, you have complete control of the output, you could sort lines, change their case, exclude them, add new ones etc, etc. You pay for that flexibility by needing to write more code to describe what you want to do. Some good example code is linked form the answer you accepted, so no point me repeating that. – Neil Slater Jul 25 '13 at 10:41
  • What would you say if i use regex? – Em Sta Jul 25 '13 at 10:58
  • @Em Sta: Using a regex instead of `delete` is just fine. So is just skipping the writing a line when you detect a line that you read that don't want in the result file. The regex, like the delete, will *not* directly alter original lines in the file you just read. To do so, you must add code that writes to the file. – Neil Slater Jul 25 '13 at 13:18
1

As others said, you can do it with grep, but if you want to do it completely in Ruby, then look at this question, the first answer should help you.

Community
  • 1
  • 1
Rafał Cieślak
  • 972
  • 1
  • 8
  • 25