How to do group Exception classes?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
 
R

rumpy

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

Emmanuel Oga

Documentation : http://www.ruby-doc.org/core-1.8.6/classes/IO.html

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
(e-mail address removed)

(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.elctech.com
 
R

rumpy

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

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top