[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to do group Exception classes?

Iñaki Baz Castillo

7/23/2008 12:29:00 AM

Hi, I've some exception that I want to group into "AuthError" class so I ca=
n=20
do a "raise" for the specific child exception or for the parent class. Is t=
he=20
following correct?

=2D--------------------------
module Auth

class GenericError < StandardError
end

class WrongPassword < GenericError
end

class ExpiredAccount < GenericError
end

end
=2D---------------------------


so now I can do:

=2D---------------------------
begin
raise Auth::WrongPassword
rescue Auth::GenericError =3D> e
puts "Exception class rescued: #{e.class}"
end

=3D> Exception class rescued: Auth::WrongPassword
=2D---------------------------

This is: I didn't do a "rescue" for Auth::WrongPassword, but for=20
Auth::GenericError.
But since Auth::WrongPassword is a child of Auth::GenericError then the res=
cue=20
is executed and "e" is Auth::WrongPassword.


Do you suggest a better way of doing it? Thanks a lot for any suggestion.


=2D-=20
I=C3=B1aki Baz Castillo

3 Answers

rumpy

7/23/2008 12:53:00 AM

0

Hi Folks,


I am attempting to search a file for a specific string, and upon
locating said string, replace the line containing the string with a
new string. No amount of reading, testing, Googling enlightened me on
how this task is accomplished using Ruby.

If this is easy in Ruby I'll proceed to start crying now because I
found my book and related internet resources woefully inadequate :(
Any help/feedback/pointers/useful links would be greatly appreciated.

Regards,

S.


Emmanuel Oga

7/23/2008 1:56:00 AM

0

Documentation : http://www.ruby-doc.org/core-1.8.6/class...

contents = File.read("filename") #read
contents.gsub!("what you look for", "the substitutive") # modify
File.open("filename", "rw") { |file| file << contents } #write

Warning: File.read will bring the whole file to memory.
--
Emmanuel Oga
ELC Technologies (TM)
1921 State Street
Santa Barbara, CA 93101
eoga@elctech.com

(866)863-7365 Tel
(866)893-1902 Fax

+44 (0) 20 7504 1346 Tel - London Office
+44 (0) 20 7504 1347 Fax - London Office

http://www.e...

rumpy

7/23/2008 5:01:00 AM

0

Emmanuel, thank you very much for sorting me out. I can see I was
close but very confused on the write step.


Quoting Emmanuel Oga <eoga@elctech.com>:

> Documentation : http://www.ruby-doc.org/core-1.8.6/class...
>
> contents = File.read("filename") #read
> contents.gsub!("what you look for", "the substitutive") # modify
> File.open("filename", "rw") { |file| file << contents } #write
>
> Warning: File.read will bring the whole file to memory.
> --
> Emmanuel Oga
> ELC Technologies (TM)
> 1921 State Street
> Santa Barbara, CA 93101
> eoga@elctech.com
>
> (866)863-7365 Tel
> (866)893-1902 Fax
>
> +44 (0) 20 7504 1346 Tel - London Office
> +44 (0) 20 7504 1347 Fax - London Office
>
> http://www.e...
>
>