Ruby has classes but they are all implemented as objects. Some objects act as classes but some of them do not. For example "string" and String are both objects -- "string" being a String class object, and String being the class. Since classes are also objects, String is still an object while "string" is not a class -- ONLY an object.
To create an instance of a class, use that class's "new" method. For example, if we had a class called "Cat" we would instantiate it using the following syntax:
Cat.new
Methods and Functions Source
Ruby doesn’t really have functions. Rather, it has two slightly different concepts - methods and Procs (which are, as we have seen, simply what other languages call function objects, or functors). Both are blocks of code - methods are bound to Objects, and Procs are bound to the local variables in scope. Their uses are quite different.
Methods are the cornerstone of object-oriented programming, and since Ruby is a pure-OO language (everything is an object), methods are inherent to the nature of Ruby. Methods are the actions Ruby objects do - the messages they receive, if you prefer the message sending idiom.
Procs make powerful functional programming paradigms possible, turning code into a first-class object of Ruby allowing to implement high-order functions. They are very close kin to Lisp’s lambda forms (there’s little doubt about the origin of Ruby’s Proc constructor lambda)
The construct of a block may at first be confusing, but it turns out to be quite simple. A block is, as my metaphor goes, an unborn Proc - it is a Proc in an intermediate state, not bound to anything yet. I think that the simplest way to think about blocks in Ruby, without losing any comprehension, would be to think that blocks are really a form of Procs, and not a separate concept. The only time when we have to think of blocks as slightly different from Procs is the special case when they are passed as the last argument to a method which may then access them using yield.
Conditional Definition:
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
Conditionals in ruby (including loops) are:
if
,elsif
,else
,unless
,when
,while
,until
, and for
.
Class Variables and Instance Variables Source
A class variable (@@) is shared among the class and all of its descendants. A class instance variable (@) is not shared by the class's descendants.
#####Class variable (@@)
Let's have a class Foo with a class variable @@i, and accessors for reading and writing @@i:
class Foo
@@i = 1
def self.i
@@i
end
def self.i=(value)
@@i = value
end
end
And a derived class:
class Bar < Foo
end
```ruby
We see that Foo and Bar have the same value for @@i:
```ruby
p Foo.i # => 1
p Bar.i # => 1
And changing @@i in one changes it in both:
Bar.i = 2
p Foo.i # => 2
p Bar.i # => 2
#####Class instance variable (@)
Let's make a simple class with a class instance variable @i and accessors for reading and writing @i:
class Foo
@i = 1
def self.i
@i
end
def self.i=(value)
@i = value
end
end
And a derived class:
class Bar < Foo
end
We see that although Bar inherits the accessors for @i, it does not inherit @i itself:
p Foo.i # => 1
p Bar.i # => nil
We can set Bar's @i without affecting Foo's @i:
Bar.i = 2
p Foo.i # => 1
p Bar.i # => 2
In OOP: A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object in a valid state. Immutable objects must be initialized in a constructor.
A ruby constructor example using initialize:
class Fruit
def initialize
@kind = "apple"
@condition = "ripe"
end
end
f4 = Fruit.new
"a ripe apple"
Superclass, Base Class source
base class/super class -- it is the parent class of a class class -- normal class subclass -- class of a class
the relation of these classes are as follows:-
BASE CLASS/SUPER CLASS---->CLASS--->SUB CLASS
example
food-----> fruit------> mango
in this
food = base class/super class fruit = class mango = sub class
Inheritance source
Inheritance is a relation between two classes. We know that all cats are mammals, and all mammals are animals. The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own. If all mammals breathe, then all cats breathe. In Ruby, a class can only inherit from a single other class. Some other languages support multiple inheritance, a feature that allows classes to inherit features from multiple classes, but Ruby doesn't support this.
Example:
class Mammal
def breathe
puts "inhale and exhale"
end
end
class Cat < Mammal
def speak
puts "Meow"
end
end
rani = Cat.new
rani.breathe
rani.speak
Encapsulation source
Encapsulation is the packing of data and functions into a single component. Encapsulation means that the internal representation of an object is hidden from the outside. Only the object can interact with its internal data. Public methods can be created to open a defined way to access the logic inside an object.
Encapsulation reduce system complexity and increase robustness by decoupling its components.
Example:
class Document
attr_accessor :name
def initialize(name)
@name = name
end
def set_name(name)
@name = name
end
end
d = Document.new('name1')
d.set_name('name1')
Polymorphism source
In programming languages and type theory, polymorphism (from Greek πολύς, polys, “many, much” and μορφή, morphē, “form, shape”) is the provision of a single interface to entities of different types.
Here’s a simple example in Ruby with inheritance :
class Document
def initialize
end
def print
raise NotImplementedError, 'You must implement the print method'
end
end
class XmlDocument < Document
def print
p 'Print from XmlDocument'
end
end
class HtmlDocument < Document
def print
p 'Print from HtmlDocument'
end
end
XmDocument.new.print # Print from XmlDocument
HtmlDocument.new.print # Print from HtmlDocument
As you can see, we sent the same message to different object and got different result. The print method is a single interface to entities of different types : XmlDocument and HtmlDocument.