[Ruby] Problem mit Stringvergleich oder stdin auslesen, nicht ganz sicher

Bender86

Lieutenant
Registriert
Jan. 2007
Beiträge
707
Hallo zusammen!

Ich habe mir ein kleines Ruby Programm geschrieben, dass in einer Datei alle Tabulatoren am Anfang jeder Zeile sucht und durch Leerzeichen ersetzt. Nun gibt es eine Abfrage (ab Zeile 43), bei der der User gefragt wird ob eine Datei überschrieben werden soll. Anschließend wird in

Code:
answer = $stdin.gets

die Antwort ausgelesen und dann überprüft was die Antwort war. Die Überprüfung schlägt aber immer fehl und bei jeder Antwort landet das Programm im else-Zweig.

Kann mir jemand sagen warum? Ist was mit dem Stringvergleich falsch? Wenn man sich "answer" ausgeben lässt ist es die Eingabe des benutzers.

Hier der Code, wichtig ist eigentlich nur der Block ab Zeile 43:

Code:
#!/usr/bin/env ruby

require 'fileutils'

=begin
  This simple programm converts all leading tabulators of each line by the
  passed file (first argument) to  whitespaces. The file itself isn't changed
  but the changes are  written to a new file "<file>_converted". A second
  argument is optional and  determines with how many whitespaces each tab is
  replaced. (default is 2 to fit with ruby conventions)
  
  Feel free to use or modify this program, but usage is at your own risk and
  without any warranty.
  
  @author  :  bender
  @version :  0.5
=end

# check if file exists, is no directory and readable

if ARGV.size < 1 || ARGV.size > 2
  $stderr.print "ERROR: bad argument count, expected: <file> [whitespaces]"
  return -1
end

if not File.exist? ARGV[0]
  $stderr.print "ERROR: could not find file \"", ARGV[0], "\"\n"
  return -1
end

if File.directory? ARGV[0]
  $stderr.print "ERROR: \"", ARGV[0], "\" is a directory\n"
  return -1
end

if not File.readable? ARGV[0]
  $stderr.print "ERROR: \"", ARGV[0], "\" is not readable\n"
  return -1
end

# check whether <file>_converted exists, if it does ask user if it shall be
# overwritten
if File.exist?(ARGV[0] + "_converted")
  $stderr.print "> there is already a file \"", ARGV[0], "_converted\"",
                " delete old one? (y)es or (n)o\n(if answer is no, the",
                " program will leave the file untouched and quit)\n"
  
  answer = $stdin.gets
  
  if answer == "y" || answer == "yes" || answer == "+" || answer == "1"
    File.delete(ARGV[0] + "_converted")
    print "> deleted file, continue with conversion"
  elsif answer == "n" || answer == "no" || answer == "-" || answer == "0"
    return 0
  else
    $stderr.print "ERROR, could not evaluate answer, assuming \"no\" was",
                      " meant and quitting the program"
  end
  
end

# open file to read and create new file for conversion
orig_file = File.open(ARGV[0], "r")
conv_file = File.new(ARGV[0] + "_converted", "a+")

# default whitespaces are 2 if the user didn't pass a second arg
space_count = 2
if ARGV[1]
  space_count = ARGV[1].to_i == 0 ? space_count : ARGV[1].to_i
end
space_str = " " * space_count

while line = orig_file.gets

  i = 0

  while line[i, 1] == "\t"
    line[i, 1] = space_str
    i += space_count
  end
  
  conv_file.puts line
end

print "> converted each tab to ", space_count.to_s, " whitespaces, check ",
      "result in \"", ARGV[0], "_converted\"\n"


P.s: @Computerbase-Team, es gibt keinen Ruby Präfix zur Auswahl! :freaky:
 
Zurück
Oben