I code my first ruby script, and it got problem!

Hello folks!

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.

···

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

Hi,

Hello folks!

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\.

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 Heesob

···

2009/3/8 Power One <arghx1@yahoo.com>:

Heesob Park wrote:

Hi,

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 Heesob

Thanks Park Heesob,

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

···

2009/3/8 Power One <arghx1@yahoo.com>:

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

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

Regards,

Park Heesob

···

2009/3/8 Power One <arghx1@yahoo.com>:

Heesob Park wrote:

Hi,

2009/3/8 Power One <arghx1@yahoo.com>:

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 Heesob

Thanks Park Heesob,

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

Once again, thank so much Park!

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 :).

Heesob Park wrote:

···

2009/3/8 Power One <arghx1@yahoo.com>:

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

Regards,

Park Heesob

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

Once again, thank so much Park!

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!

If you did Dir.chdir("#{@dirname}"), the current working directory is
"#{@dirname}".
So, the next Dir.delete("#{@dirname}") fails to 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

According to http://www.dweebd.com/ruby/temporarily-change-ruby-working-directory/
The above can be written as

   def see_me
     if File.exist?("#{@dirname}")
         Dir.chdir("#{@dirname}") do
           puts Dir.pwd
         end
     else
       puts "This directory #{@dirname} is not exist!"
     end
   end

Regards,

Park Heesob

···

2009/3/9 Power One <arghx1@yahoo.com>:

# if File.exist?("#{@dirname}")

try,

   if File.directory? @dirname

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...

···

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

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 Heesob

···

2009/3/9 Power One <arghx1@yahoo.com>:

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...

Heesob Park wrote:

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 Heesob

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

···

2009/3/9 Power One <arghx1@yahoo.com>:

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