Hi All,
I am not a programmer, yet. I am trying ruby on for size, most things
fit ok, but Classes are puzzling me.
class Stats
Hi All,
I am not a programmer, yet. I am trying ruby on for size, most things
fit ok, but Classes are puzzling me.
class Stats
Classes in any OOP (object orientated language) follows the same basic
laws
of application.
A class would be the actual application or legal body of code. It must
be
noted that within a class body you can deï¬ne class methods and instance
methods. This is what allows the application to actually function or
operate.
Ruby EXAMPLE:
class Greeter
def initialize(name)
@name = name
end
def say(phrase)
puts "#{phrase}, #{@name}"
end
end
g1 = Greeter.new(“Fred”)
g2 = Greeter.new(“Wilma”)
g1.say(“Hello”) #=> Hello, Fred
g2.say(“Hi”) #=> Hi, Wilma
Java EXAMPLE:
class HelloWorld {
public static void main(String [] args) {
System.out.println(“hello world!!!”);
Inheritance is the concept of a child class (sub class) automatically
inheriting the variables and methods
defined in its parent class (super class) This is known as inheritence.
EXAMPLE:
The Shape class would be the parent class, and from that the circle,
square, and rectangle classes would be created or inherited.
The Human class would be the the parent class, and from that the
man,
and woman classes would be created or inherited.
*Super class (Parent class) *
– Any class above a specific class in the class hierarchy.
Sub class (Child class)
– Any class below a specific class in the class hierarchy.
*In Ruby
*Class deï¬nitions start with the keyword class followed by the class
name
(which
must start with an uppercase letter).
Below is a class called MyClass.
class MyClass
def m1 # this method 1 is public
end
protected
def m2 # this method 2 is protected
end
private
def m3 # this method 3 is private
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs