-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial1_class_objects.rb
72 lines (55 loc) · 1.11 KB
/
tutorial1_class_objects.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#defining a class in ruby
#always start with class followed by class name and end with 'end'
class Custumer
end
=begin
Types of variables in Ruby
=end
#Local variables
#available within the 'method' only
#Examples
# name= 'Ernes'
# _age = 4
#Instance variables
#available across instance methods or objects
#Examples
# @name
# @age
class Student
@student_number
end
#Class Variable
#available across all objects
# Examples
# @@name
#@@age
class Person
@@no_of_people = 0
end
#Global variables
#available across the whol script
#Examples
# $name
$file_name = 'ruby'
=begin
Creating a new instance works as follows
=end
cust1 = Custumer.new
=begin
class with constructor is defined with the
initialize method
=end
class Vehicle
#This is a static or class variable
@@number_of_cars = 0
#This line defines the constructor
def initialize(id, veh_name, model)
#these are the data fields or data members
@vehicle_id = id
@vehicle_model = model
@brand_name = veh_name
end
end
#Creating new objects with 'No-default' constructor
bmw = Vehicle.new(1,"bwm", "2series")
mazda = Vehicle.new(2,"mazda", "cx3")