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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-08 09:54
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.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-08 10:06
(Received via mailing list)
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
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-08 10:38
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
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-08 13:55
(Received via mailing list)
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
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-08 18:11
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
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-09 02:22
(Received via mailing list)
2009/3/9 Power One <arghx1@yahoo.com>:
> 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.

>>     else
>>       puts "This directory #{@dirname} is not exist!"
>>     end
>>   end
>>
According to
http://www.dweebd.com/ruby/temporarily-change-ruby...
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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2009-03-09 03:09
(Received via mailing list)
#   if File.exist?("#{@dirname}")

try,

   if File.directory? @dirname
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-09 04:10
(Received via mailing list)
Peña wrote:
> #   if File.exist?("#{@dirname}")
>
> try,
>
>    if File.directory? @dirname

Might @dirname be nil ?

That was the only reason I could think for the string masher.

@dirname.to_s has the same powers...
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-09 10:15
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...
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-09 10:37
(Received via mailing list)
2009/3/9 Power One <arghx1@yahoo.com>:
> 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
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-09 10:44
Heesob Park wrote:
> 2009/3/9 Power One <arghx1@yahoo.com>:
>> 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
This topic is locked and can not be replied to.