[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Power One

3/8/2009 8:52:00 AM

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.....
--
Posted via http://www.ruby-....

10 Answers

Heesob Park

3/8/2009 9:04:00 AM

0

Hi,

2009/3/8 Power One <arghx1@yahoo.com>:
> 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.....
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

Power One

3/8/2009 9:37:00 AM

0

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

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

Heesob Park

3/8/2009 12:53:00 PM

0

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?". =C2=
=A0How
>>> can I solve this problem? =C2=A0You can see my script at
>>> http://pastie.....
>> You actually created directory not "land?" but "land\n"
>>
>> The line # 5 of your code
>> =C2=A0 if @dirname =3D gets
>> should be
>> =C2=A0 if @dirname =3D 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
> =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

Power One

3/8/2009 5:10:00 PM

0

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

Heesob Park

3/9/2009 1:20:00 AM

0

2009/3/9 Power One <arghx1@yahoo.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? =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.

>
>> =C2=A0 def see_me
>> =C2=A0 =C2=A0 if File.exist?("#{@dirname}")
>> =C2=A0 =C2=A0 =C2=A0 cwd =3D Dir.pwd
>> =C2=A0 =C2=A0 =C2=A0 begin
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 Dir.chdir("#{@dirname}")
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 puts Dir.pwd
>> =C2=A0 =C2=A0 =C2=A0 ensure
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 Dir.chdir(cwd)
>> =C2=A0 =C2=A0 =C2=A0 end
>> =C2=A0 =C2=A0 else
>> =C2=A0 =C2=A0 =C2=A0 puts "This directory #{@dirname} is not exist!"
>> =C2=A0 =C2=A0 end
>> =C2=A0 end
>>
According to http://www.dweebd.com/ruby/temporarily-change-ruby-wo...
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

Peña, Botp

3/9/2009 2:07:00 AM

0

IyAgIGlmIEZpbGUuZXhpc3Q/KCIje0BkaXJuYW1lfSIpDQoNCnRyeSwNCg0KICAgaWYgRmlsZS5k
aXJlY3Rvcnk/IEBkaXJuYW1lDQoNCg==

Phlip

3/9/2009 3:05:00 AM

0

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

Power One

3/9/2009 9:14:00 AM

0

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

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

Heesob Park

3/9/2009 9:35:00 AM

0

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

Power One

3/9/2009 9:43:00 AM

0

Heesob Park wrote:
> 2009/3/9 Power One <arghx1@yahoo.com>:
>> Updated script is at http://pastie.....
>> `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
--
Posted via http://www.ruby-....