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

P

Power One

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

Heesob Park

Hi,

2009/3/8 Power One said:
Hello folks!

I'm totally new to Ruby. =C2=A0For practice, I'm trying to code a very si= mple
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. =C2=A0For ex: =C2=A0if user enter directory /home/disney/land, i= nstead of
creating directory "land", the script creates a directory "land?". =C2=A0= How
can I solve this problem? =C2=A0You 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 =3D gets
should be
if @dirname =3D gets.chomp


Regards,

Park Heesob
 
P

Power One

Heesob said:
Hi,


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
 
H

Heesob Park

2009/3/8 Power One said:
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
=C2=A0 =C2=A0if File.exist?("#{@dirname}")
=C2=A0 =C2=A0 =C2=A0puts "I'm going to delete your directory #{@dirname}"
=C2=A0 =C2=A0 =C2=A0Dir.delete("#{@dirname}")
=C2=A0 =C2=A0 =C2=A0puts "#{@dirname} has been deleted."
=C2=A0 =C2=A0else
=C2=A0 =C2=A0 =C2=A0puts "#{@dirname} is still here!"
=C2=A0 =C2=A0end
=C2=A0 =C2=A0end
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 =3D 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
 
P

Power One

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

Heesob Park

2009/3/9 Power One said:
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? =C2=A0I s= aw
that you have to use cwd =3D 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.
According to http://www.dweebd.com/ruby/temporarily-change-ruby-working-dir=
ectory/
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
 
P

Peña, Botp

IyAgIGlmIEZpbGUuZXhpc3Q/KCIje0BkaXJuYW1lfSIpDQoNCnRyeSwNCg0KICAgaWYgRmlsZS5k
aXJlY3Rvcnk/IEBkaXJuYW1lDQoNCg==
 
P

Power One

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

Heesob Park

2009/3/9 Power One said:
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! =C2=A0Why? = =C2=A0Linux
already has ways for you to remove, delete, and messing with files
without any help from a wrapper or any external programs! =C2=A0LOL, 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! =C2=A0I don't know how to fix it at this
point. =C2=A0New error is complaining within the method "file_remove", an= d
the error is

/home/geek/My_Ruby_01/TestTheRuby/test/Test_Dir.rb:103:in `list_file':
type mismatch: String given (TypeError)
=C2=A0 =C2=A0 =C2=A0 =C2=A0from /home/spidy/My_Ruby_01/TestTheRuby/test/T= est_Dir.rb:103:in
`file_remove'
=C2=A0 =C2=A0 =C2=A0 =C2=A0from /home/geek/My_Ruby_01/TestTheRuby/test/Te= st_Dir.rb:81:in
`del_dir'
=C2=A0 =C2=A0 =C2=A0 =C2=A0from /home/geek/My_Ruby_01/TestTheRuby/test/Te= st_Dir.rb:141

Anyone dare to give me any advice further? LOL...

def list_file(file)
@filex =3D=3D file
while @filex.to_s =3D~ "ls files"
Dir.entries(".").join(' ')
end
end

should be

def list_file(file)
@filex =3D file
if @filex =3D=3D "ls files"
puts Dir.entries(".").join(' ')
end
end


Regards,

Park Heesob
 
P

Power One

Heesob said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top