Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Ruby

2,663 views

Published on

an introduction to ruby programming language. This is a start for peoples those who start ruby on rails

Published in: Technology, Education

Introduction to Ruby

  1. 1. Ruby Programming
  2. 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. 3. Advantages • Powerful And Expressive • Rich Library Support • Rapid Development • Open Source • Flexible and Dynamic
  4. 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 #
  5. 5. Types • Command “ruby” • The extension is .rb #
  6. 6. Ruby Documents • Command ri • Example – ri class – #
  7. 7. helloPerson.rb • # First ruby programme • def helloPerson(name) • result = "Hello, " + name • return result • end • puts helloPerson("Justin") #
  8. 8. Execute Programme • $ ruby helloPerson.rb • Hello, Justin • Nice and simple • Can use irb – interactive ruby shell • # is for comments like // #
  9. 9. Ruby variables • def returnFoo • bar = "Ramone, bring me my cup." • return bar • end • puts returnFoo • #
  10. 10. Kinds of Variables • Global variable - $ sign • instance variable - @ sign • Class Variable - @@ sign • Local Variable – no sign • Constants – Capital Letters #
  11. 11. Global Variable • Available everywhere inside a programme • Not use frequently #
  12. 12. instance variable • Unique inside an instance of a class • Truncated with instance • @apple = Apple.new • @apple.seeds = 15 • @apple.color = "Green" • #
  13. 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. 14. Classes • Initialize – is the constructor • Def – end -> function • Class-end -> class #
  15. 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. 16. Define Object • @student = Student.new • @student.delete_students # This will fail • Because it is private • #
  17. 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. 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. 19. Inheritance • Inherit a parent class • Extend functions and variables • Add more features to base class #
  20. 20. Polymorphism • The behavior of an object that varies depending on the input. • • #
  21. 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. 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. 23. Calling Objects • @course1 that contains information about a Course • @course2 is another instance variable, but it contains information about a GradClass object • #
  24. 24. Arrays and hashes • fruit = ['Apple', 'Orange', 'Squash'] • puts fruit[0] • fruit << 'Corn' • puts fruit[3] #
  25. 25. Arrays • << will input a new element • Last line outputs the new element #
  26. 26. Arrays More... • fruit = { • :apple => 'fruit', • :orange => 'fruit', • :squash => 'vegetable' • } • puts fruit[:apple] • fruit[:corn] = 'vegetable' • puts fruit[:corn] #
  27. 27. Arrays More... • h = {"Red" => 1, "Blue" => 2, "Green" => 3} • CORPORATE • p h["Red"] • Outpus -> 1 • h["Yellow"] = 4 • p h["Yellow"] • Outputs -> 4 #
  28. 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. 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. 30. Iterators • fruit = ['Apple', 'Orange', 'Squash'] • fruit.each do |f| • puts f • end #
  31. 31. Iterators • Keyword - do - • Instance variable |f| • Print f means print the instance of the loop #
  32. 32. Iterators • fruit = ['Apple', 'Orange', 'Squash'] • fruit.each_with_index do |f,i| • puts "#{i} is for #{f}" • end • #
  33. 33. Iterators • Here f is the instance • Index is i • Will get two variables #
  34. 34. Iterators • fruit = ['Apple', 'Orange', 'Squash'] • for i in 0...fruit.length • puts fruit[i] • end • #
  35. 35. Iterators • For loop • Same old syntax • But 'each' loop is smart to handle an array • 'each' dont need a max cutoff value. #
  36. 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. 37. Exception handling • begin • @user = User.find(1) • @user.name • rescue • STDERR.puts "A bad error occurred" • end • #
  38. 38. Thanks #

×