I’m totally new to Ruby. For practice, I’m trying to code a very simple
script that checks user’s input for directory name, and then check to
see if directory is exist, if not create it, and then clean it up by
delete the created directory.
Unfortunately, the script didn’t create correct directory as it always
attach a question-mark right behind a directory name that a user
entered. For ex: if user enter directory /home/disney/land, instead of
creating directory “land”, the script creates a directory “land?”. How
can I solve this problem? You can see my script at http://pastie.org/410753.
creating directory “land”, the script creates a directory “land?”. Â How
can I solve this problem? Â You can see my script at http://pastie.org/410753.
You actually created directory not “land?” but “land\n”
The line # 5 of your code
if @dirname = gets
should be
if @dirname = gets.chomp
creating directory “land”, the script creates a directory “land?”. Â How
can I solve this problem? Â You can see my script at http://pastie.org/410753.
You actually created directory not “land?” but “land\n”
The line # 5 of your code
if @dirname = gets
should be
if @dirname = gets.chomp
Regards,
Park H.
Thanks Park H.,
gets.chomp did the job perfectly, but my very last code is like @letst.del_dir which is calling the “def del_dir” is not working.
I think Dir.delete is the wrong way to use for deleting a directory?
def del_dir
if File.exist?(“#{@dirname}”)
puts “I’m going to delete your directory #{@dirname}”
Dir.delete(“#{@dirname}”)
puts “#{@dirname} has been deleted.”
else
puts “#{@dirname} is still here!”
end
end
 if @dirname = gets
gets.chomp did the job perfectly, but my very last code is like
   puts “#{@dirname} is still here!”
  end
  end
The del_dir method has no problem.
The problem is due to see_me method.
Modify see_me something like this will work.
def see_me
if File.exist?(“#{@dirname}”)
cwd = Dir.pwd
begin
Dir.chdir(“#{@dirname}”)
puts Dir.pwd
ensure
Dir.chdir(cwd)
end
else
puts “This directory #{@dirname} is not exist!”
end
end
I wonder why do I have to use ensure to have the subsequent method
del_dir to work?
After seeing you used ensure, I read on the Internet, it says that
ensure is helping to make sure the code block within ensure will always
be executed.
Though when I do Dir.chdir(“#{@dirname}”), isn’t this adequate? I saw
that you have to use cwd = Dir.pwd, and then do Dir.chdir, and then do
ensure again!
Just out of curiosity as I really want to understand Ruby’s process.
Thank You so much, please help explain :).
 if @dirname = gets
gets.chomp did the job perfectly, but my very last code is like
   puts “#{@dirname} is still here!”
  end
  end
The del_dir method has no problem.
The problem is due to see_me method.
Modify see_me something like this will work.
def see_me
if File.exist?(“#{@dirname}”)
cwd = Dir.pwd
begin
Dir.chdir(“#{@dirname}”)
puts Dir.pwd
ensure
Dir.chdir(cwd)
end
else
puts “This directory #{@dirname} is not exist!”
end
end
Updated script is at http://pastie.org/411503.
`del_dir’
    from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:141
Anyone dare to give me any advice further? LOL…
def list_file(file) @filex == file
while @filex.to_s =~ “ls files”
Dir.entries(“.”).join(’ ')
end
end
should be
def list_file(file) @filex = file
if @filex == “ls files”
puts Dir.entries(“.”).join(’ ')
end
end
Regards,
Park H.
Thanks Park for catching that.
After that fix, I still have the same error as such:
home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:103:in list_file': type mismatch: String given (TypeError) from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:103:in file_remove’
from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:81:in
`del_dir’
from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:141
What was first a simple stupid script that I try to whip up for practice
is not getting complicated, but it’s still a stupid script! Why? Linux
already has ways for you to remove, delete, and messing with files
without any help from a wrapper or any external programs! LOL, but
since I’m a total noob to Ruby, and still learning the syntax, plus I’m
not a programmer, and Ruby is like my first language, and so I dig a
bigger hole to sink in even worse.
The original script was like 39 lines, and it has now turned into 141
lines or so.
Updated script is at http://pastie.org/411503.
Still new errors got introduced! I don’t know how to fix it at this
point. New error is complaining within the method “file_remove”, and
the error is
/home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:103:in list_file': type mismatch: String given (TypeError) from /home/spidy/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:103:in file_remove’
from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:81:in
`del_dir’
from /home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:141
Anyone dare to give me any advice further? LOL…
Thanks…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.