Download presentation
Presentation is loading. Please wait.
1
CIT 383: Administrative Scripting
Classes CIT 383: Administrative Scripting
2
CIT 383: Administrative Scripting
Topics Creating a Class Initialization Accessors and Attributes Redefining Methods Equality Class Variables CIT 383: Administrative Scripting
3
CIT 383: Administrative Scripting
What is a Class? A class is a blueprint to create objects Attributes: named values associated with object Methods: actions that can be performed on object. A Point object has Attributes: x and y coordinates Methods: x to read x-coordinate y to read y-coordinate to_s to return a string representation CIT 383: Administrative Scripting
4
CIT 383: Administrative Scripting
Why Create Classes? Ruby provides many classes Array DateTime File Regexp String But it doesn’t provide classes for your domain ApacheStatus AutomateIt PasswdCheck Uptime VMstats CIT 383: Administrative Scripting
5
CIT 383: Administrative Scripting
Creating a Class A class is defined with the class keyword class Point end Instances of the class are created with new p = Point.new The instance has methods from Object class p.class # Point p.is_a? Point # true CIT 383: Administrative Scripting
6
Initializing an Object
The initialize method is run during new class Point def initialize(x,y) = x, y end Instance variables used with prefix Instance variables belong to a particular object. Only that object can access those values. CIT 383: Administrative Scripting
7
CIT 383: Administrative Scripting
Methods Methods are defined with the def keyword. class Point def x x end Methods are called using the . syntax. p = Point.new(0,0) xvalue = p.x CIT 383: Administrative Scripting
8
CIT 383: Administrative Scripting
Assignment Methods Use the = operator using method ending in = class Point def x=(value) @x = value end Methods are called using the . syntax. p = Point.new(0,0) p.x = 10 CIT 383: Administrative Scripting
9
CIT 383: Administrative Scripting
Accessor Shortcuts Let Ruby create accessor methods automatically. attr_reader: create read-only methods attr_writer: create assignment write methods attr_accessor: create read and write methods. Examples class Point attr_accessor :x, :y end p = Point.new(0,0) p.x p.y = 1 CIT 383: Administrative Scripting
10
CIT 383: Administrative Scripting
Operator Methods Can define operators like == for class too. class Point def ==(o) if o.is_a? Point @x==o.x else false end Example p1 = Point.new(1,1) p2 = Point.new(1,2) p1 == p # false CIT 383: Administrative Scripting
11
CIT 383: Administrative Scripting
Redefining Methods Can redefine built-in methods like to_s class Point def to_s end Example: p = Point.new(1,2) puts p # “(1,2)” CIT 383: Administrative Scripting
12
CIT 383: Administrative Scripting
Class Variables Class variables are shared by all objects of class. Use differ from instance vars which belong to one object. Example: Track number of objects of class created. class Point = 0 def initialize(x,y) = x, y += 1 end def count p1 = Point.new(0,0); p2 = Point.new(1,1) p2.count # returns 2 CIT 383: Administrative Scripting
13
CIT 383: Administrative Scripting
References Michael Fitzgerald, Learning Ruby, O’Reilly, David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. Hal Fulton, The Ruby Way, 2nd edition, Addison- Wesley, 2007. Robert C. Martin, Clean Code, Prentice Hall, Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005. CIT 383: Administrative Scripting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.