4 Classess Modules and Exception
4 Classess Modules and Exception
Class Principles
4.4. Modules
4.5. Exceptions
4.6. Conclusion
This means that any value is an object and any object has its own
class (it is an instance of that class).
A class defines what an object will look like, i.e. the set of methods
that the object accepts.
• What is a class ?
• What is an object ?
• What is an instance of a class ?
• What is a method ?
• What is a subclass ?
object.method_name
The only new concepts are class creation and object instantiation
but they are very simple.
http://ruby-doc.org/core-1.9.3/Class.html
Note they are defined within class definition using the special
character @.
We have already said that self keyword always refers the current
object.
So we can define methods that belong to the class object using it.
They are encapsulated in the object that represent the class; the
user of a class cannot access them from outside without
getter/setter.
https://en.wikipedia.org/wiki/Reflection_(computer_programming)
When you define new data types, you want to define operations
and operators for that type too, and you may want to use pre-
defined and easy syntax operator like +, -, *, etc.
The sum of two points will return a new point that contains the
sum of the coordinates x and y.
https://en.wikipedia.org/wiki/Reflection_(computer_programming)
In Ruby, a class does not define the set of fields for its object
instances like Java or other OO languages.
The redefinition does not affect the superclass and its object.
Ruby first looks up a class constant in its lexical scope rather then
in the inherited chain. An inherited method (not overridden)
always uses the superclass constant even if it is overridden in the
subclass.
When you work with inheritance, you must know that private
keyword works in a similar way as the protected Java keyword.
The only difference is that you have to use them in an explicit way
(object.method notation) when used inside (and not outside) a
class or subclass.
http://ruby-doc.org/core-1.9.3/Module.html
http://ruby-doc.org/core-2.1.0/Math.html
You use them by including the third party custom module and they
provide you a hierarchy of methods, constants, classes and more.
If your class defines the operator <=>, you can include Comparable
to get for free operation like <, <=, ==, >, >= and between?.
The Ruby platform provides other usable classes as well. Each one
requires that your target class implements some methods in order
to work correctly. For example, Enumerable requires the method
each and <=> for some operations.
But as with almost all OO languages. Ruby allows you to handle the
error and execute some arbitrary code.
http://ruby-doc.org/core-1.9.3/Exception.html
If you want to use a custom error and non pre-defined errors are
ideal, it is best practice to create your Error class that is inherited
from StandardError.
Please consult the following reference for the full hierarchy of pre-
defined Error classes.
http://ruby-doc.org/core-1.9.3/Exception.html
http://ruby-doc.org/core-1.9.3/Exception.html
• else
• ensure
Using else is similar to putting the else block at the end of the
begin clause. Note that any exception raised in the else block will
not be handled by the rescue clause!
begin
# code You may need it when
rescue
# code you become a Ruby
else
# code guru.
end
You can use all the previous concepts and clauses with methods,
classes and modules as well without the need of begin keyword.
def my_method(a,b,c)
# normal flow
rescue
# exception handling
else
# no exception occur
ensure
# always executed
end
Exceptions are a very powerful Ruby feature. You can write elegant
and semantically correct code that are able to handle all type of
errors and exceptions.
Now you should be able to understand how Ruby works and you
should be able to write simple and rich Ruby program.
Therefore in the next sections, you will learn system and network
programming with Ruby.
You will also see how to use Ruby in order to write a custom
Metasploit module and much more.
https://www.techotopia.com/index.php/Rub https://en.wikibooks.org/wiki/Ruby_Program
y_Essentials ming