2. What is Ruby ?
• Ruby – Object Oriented Programming
Language
• Written 1995 by Yukihiro Matsumoto
• Influenced by Python,Pearl,LISP
• Easy to understand and workwith
• Simple and nice syntax
• Powerful programming capabilities
#
3. Advantages
• Powerful And Expressive
• Rich Library Support
• Rapid Development
• Open Source
• Flexible and Dynamic
4. Install Ruby
• On Fedora
– Rpms are available
• ruby-1.8.6.287-8.fc11.i586
• ruby-devel
• ruby-postgres
• ruby-docs
• ruby-racc
• ruby-docs
#
12. instance variable
• Unique inside an instance of a class
• Truncated with instance
• @apple = Apple.new
• @apple.seeds = 15
• @apple.color = "Green"
•
#
13. • class Course
Classes
• def initialize(dept, number, name, professor)
• @dept = dept
• @number = number
• @name = name
• @professor = professor
• end
• def to_s
• "Course Information: #@dept #@number - #@name [#@professor]"
• end
• def
• self.find_all_students
• ...
• end
• end
#
14. Classes
• Initialize – is the constructor
• Def – end -> function
• Class-end -> class
#
15. Define Object
• class Student
• def login_student
• puts "login_student is running"
• end
• private
• def delete_students
• puts "delete_students is running"
• end
• protected
• def encrypt_student_password
• puts "encrypt_student_password is running"
• end
• end
#
16. Define Object
• @student = Student.new
• @student.delete_students # This will fail
• Because it is private
•
#
17. Classes consist of methods
and instance variables
• class Coordinate
• def initialize(x,y) #constructor
• @x = x # set instance variables
• @y = y
• end
• def to_s # string representation
• "(#{@x},#{@y})"
• end
• end
• point = Coordinate.new(1,5)
• puts point
• Will output (1,5) #
18. Inheritance
• class AnnotatedCoordinate < Coordinate
• def initialize(x,y,comment)
• super(x,y)
• @comment = comment
• end
• def to_s
• super + "[#@comment]"
• end
• End
• a_point =
• AnnotatedCoordinate.new(8,14,"Centre");
• puts a_point
• Out Put Is -> (8,14)[Centre]
#
19. Inheritance
• Inherit a parent class
• Extend functions and variables
• Add more features to base class
#
21. Polymorphism
• class Person
• # Generic features
• end
• class Teacher < Person
• # A Teacher can enroll in a course for a semester as either
• # a professor or a teaching assistant
• def enroll(course, semester, role)
• ...
• end
• end
• class Student < Person
• # A Student can enroll in a course for a semester
• def enroll(course, semester)
• ...
• end
• end #
22. Calling objects
• @course1 = Course.new("CPT","380","Beginning
Ruby Programming","Lutes")
• @course2 = GradCourse.new("CPT","499d","Small
Scale Digital Imaging","Mislan", "Spring")
• p @course1.to_s
• p @course2.to_s
#
23. Calling Objects
• @course1 that contains information
about a Course
• @course2 is another instance variable,
but it contains information about a
GradClass object
•
#
24. Arrays and hashes
• fruit = ['Apple', 'Orange', 'Squash']
• puts fruit[0]
• fruit << 'Corn'
• puts fruit[3]
#
25. Arrays
• << will input a new element
• Last line outputs the new element
#
28. Decision structures
• age = 40
• if age < 12
• puts "You are too young to play"
• elsif age < 30
• puts "You can play for the normal price"
• elsif age == 35
• puts "You can play for free"
• elsif age < 65
• puts "You get a senior discount"
• else
• puts "You are too old to play"
• end
#
29. while
• clock = 0
• while clock < 90
• puts "I kicked the ball to my team mate
in the " + count.to_s + "
• minute of the match."
• clock += 1
• end
#
30. Iterators
• fruit = ['Apple', 'Orange', 'Squash']
• fruit.each do |f|
• puts f
• end
#
31. Iterators
• Keyword - do -
• Instance variable |f|
• Print f means print the instance of the
loop
#
32. Iterators
• fruit = ['Apple', 'Orange', 'Squash']
• fruit.each_with_index do |f,i|
• puts "#{i} is for #{f}"
• end
•
#
33. Iterators
• Here f is the instance
• Index is i
• Will get two variables
#
34. Iterators
• fruit = ['Apple', 'Orange', 'Squash']
• for i in 0...fruit.length
• puts fruit[i]
• end
•
#
35. Iterators
• For loop
• Same old syntax
• But 'each' loop is smart to handle an
array
• 'each' dont need a max cutoff value.
#
36. case...when
• temperature = -88
• case temperature
• when -20...0
• puts "cold“; start_heater
• when 0...20
• puts “moderate"
• when 11...30
• puts “hot”; drink_beer
• else
• puts "are you serious?"
• end
#
37. Exception handling
• begin
• @user = User.find(1)
• @user.name
• rescue
• STDERR.puts "A bad error occurred"
• end
•
#