presentation - Slide 1

508 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
508
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

presentation - Slide 1

  1. 1. RUBY By Kevin Dugal
  2. 2. What is Ruby? • Sorry Harold, it is not •Object Oriented Programming Language •Takes elements from Perl and Smalltalk.
  3. 3. History of Ruby  Developed by Yukihiro Matsumoto in 1995.  Wanted a language that was mix of functional and imperative languages.  Wanting more scripting than Perl and more object oriented than Python.  Freeware and is ranked the 9th most used in the world.  Open-source.  Applications:  Lots of web and network uses ○ Ruby on Rails: Twitter, Hulu, Urban Dictionary, etc.
  4. 4. Design of Ruby  Works via command line, like other scripting languages.  Can even write program and run directly in prompt ○ % ruby -e 'puts "hello world"' hello world  Everything in Ruby is treated as an object and all functions are methods.  This includes “+” and definitions of a class. ○ Thus a + b is treated as a.add(b)
  5. 5. Data Types  Every data type is an object of a class  No primitive types  No declaration of types when defining vars.  Contains all the basic types:  Ints, doubles, floats, strings, chars, boolean ○ 0, “”, [] all evaluate to true for boolean values
  6. 6. Arrays + Regular Expressions  Arrays can hold various types.  [1,2,”CS320”, x] is a valid array.  Regular expressions can be written in Ruby as we would in class:  ruby> def chab(s) # "contains hex in angle brackets" | (s =~ /<0(x|X)(d|[a-f]|[A-F])+>/) != nil | end nil ruby> chab "Not this one." false ruby> chab "Maybe this? {0x35}" # wrong kind of brackets false ruby> chab "Or this? <0x38z7e>" # bogus hex digit false ruby> chab "Okay, this: <0xfc0004>." true
  7. 7. Flow Control  If, then  if cond **code** [else **code**]  No groupings  Case:  Similar to C, uses === instead of ==  Loops: same as in Python  But, with the use of iterators such as each_byte, each_index, and each_line, we will rarely need to write a loop explicitly. kind = case year when 1850..1889 then "Blues" when 1890..1909 then "Ragtime" when 1910..1929 then “N.O. Jazz" when 1930..1939 then "Swing" when 1940..1950 then "Bebop" else "Jazz" end
  8. 8. Example of Code  iterator for Fibonacci numbers def fibUpTo(max) n1, n2 = 1, 1 while n1 <= max yield n1 # invoke block with passing value n1, n2 = n2, n1+n2 # and calculate next end end fibUpTo(1000) { |term| print term, " " } produces: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
  9. 9. Variables  Writing scheme:  First character ○ $ -- global ○ @ -- instance ○ Lower case – local var. ○ Upper case – constant  Unlike Java, instance variables are not observable outside the class without special methods called accessors.  attr_reader :v def v; @v; end  attr_writer :v def v=(value); @v=value; end  attr_accessor :v attr_reader :v; attr_writer :v  attr_accessor :v, :w attr_accessor :v; attr_accessor :w  Also, instance variables not declared separately, but inside methods.
  10. 10. Methods (finally!)  As in Java, use the “dot” notation.  Can call methods for single objects on an array of the objects. “maps”  Parentheses not necessary if not ambiguous  Writing methods:  Start with def **name** (**args**)  Constructor is called “initialize”  No curly braces, but the “def” is paired to “end”  As mentioned before, the declaration of a class is itself a method, but with the structure ○ class **name** **code** end
  11. 11. Classes  Make a new object in class:  E.g. kevin = Student.new  We can even redefine methods for singular object belonging to the same class, without creating a new class.  Inheritance:  Class dog < mammal  Only can inherit one class.
  12. 12. Modules and Mixins  Modules are a collections of functions with no subclasses or instances.  Example: Math library for sqrt function or PI constant.  Mixins can allow multiple inheritance, kinda…  # Base Number class class Number def intValue @value end end # BigInteger extends Number class BigInteger < Number # Add class methods from Math extend Math # Add a constructor with one parameter def initialize(value) @value = value end end
  13. 13. More Example Code class Person def initialize(lname, fname) @lname = lname @fname = fname end def lname return @lname end def fname return @fname end def lname=(myarg) @lname = myarg end def fname=(myarg) @fname = myarg end end
  14. 14. Even More Example Code class Total def initialize(initial_amount) @total=initial_amount end def setName(name) @name = name; end def hasName() return @name != nil; end def getName() return @name; end def increaseBy(increase) @total += increase end def multiplyBy(increase) @total *= increase end def setTo(amount) @total = amount end def getTotal() return @total; end def hasTotal() return @total!=nil; end end
  15. 15. Sources  “Ruby Basic Tutorial.” Updated Oct. 2006. Retrieved on April 30, 2010. http://www.troubleshooters.com/codecorn/ruby/basictut orial.htm  “Ruby Programming Language.” Retrieved on May 3, 2010. Updated 2010. http://www.ruby-lang.org/en/  “Ruby (programming language)” Updated March 15, 2010. Retrieved on May 3, 2010. http://en.wikipedia.org/wiki/Ruby_(programming_langua ge).  Slagell, Mark. “What Is Ruby?” Updated 2009. Retrieved May 5, 2010. http://www.rubyist.net/~slagell/ruby/index.html

×