How to do group Exception classes?

Hi, I’ve some exception that I want to group into “AuthError” class so I
can
do a “raise” for the specific child exception or for the parent class.
Is the
following correct?


module Auth

class GenericError < StandardError
end

class WrongPassword < GenericError
end

class ExpiredAccount < GenericError
end

end

so now I can do:


begin
raise Auth::WrongPassword
rescue Auth::GenericError => e
puts “Exception class rescued: #{e.class}”
end

=> Exception class rescued: Auth::WrongPassword

This is: I didn’t do a “rescue” for Auth::WrongPassword, but for
Auth::GenericError.
But since Auth::WrongPassword is a child of Auth::GenericError then the
rescue
is executed and “e” is Auth::WrongPassword.

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

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 :frowning:
Any help/feedback/pointers/useful links would be greatly appreciated.

Regards,

S.

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 O.
ELC Technologies ™
1921 State Street
Santa Barbara, CA 93101
[email protected]

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

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

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

Quoting Emmanuel O. [email protected]:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs