• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to Ruby
 

Introduction to Ruby

on

  • 2,191 views

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

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

Statistics

Views

Total Views
2,191
Views on SlideShare
2,112
Embed Views
79

Actions

Likes
4
Downloads
70
Comments
1

4 Embeds 79

http://smashingweb.ge6.org 26
http://smashingweb.info 22
http://ranjith.zfs.in 20
http://www.slideshare.net 11

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Introduction to Ruby Introduction to Ruby Presentation Transcript

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