presentation - slide 1

15
RUBY By Kevin Dugal

Upload: newbu

Post on 20-Jan-2015

243 views

Category:

Documents


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: presentation - Slide 1

RUBY

By Kevin Dugal

Page 2: presentation - Slide 1

What is Ruby?

• Sorry Harold, it is not

•Object Oriented Programming Language• Takes elements from Perl and Smalltalk.

Page 3: presentation - Slide 1

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.

Page 4: presentation - Slide 1

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)

Page 5: presentation - Slide 1

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

Page 6: presentation - Slide 1

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  nilruby> chab "Not this one."  falseruby> chab "Maybe this? {0x35}"    # wrong kind of brackets  falseruby> chab "Or this? <0x38z7e>"    # bogus hex digit  falseruby> chab "Okay, this: <0xfc0004>."  true

Page 7: presentation - Slide 1

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

Page 8: presentation - Slide 1

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

Page 9: presentation - Slide 1

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.

Page 10: presentation - Slide 1

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

Page 11: presentation - Slide 1

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.

Page 12: presentation - Slide 1

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 classclass 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

Page 13: presentation - Slide 1

More Example Codeclass Person

def initialize(lname, fname)@lname = lname@fname = fnameend

def lnamereturn @lnameend

def fnamereturn @fnameend

def lname=(myarg)@lname = myargend

def fname=(myarg)@fname = myargend

end

Page 14: presentation - Slide 1

Even More Example Codeclass Total

def initialize(initial_amount)@total=initial_amountend

def setName(name) @name = name; enddef hasName() return @name != nil; enddef getName() return @name; end

def increaseBy(increase)@total += increaseend

def multiplyBy(increase)@total *= increaseend

def setTo(amount)@total = amountend

def getTotal() return @total; enddef hasTotal() return @total!=nil; end

end

Page 15: presentation - Slide 1

Sources “Ruby Basic Tutorial.” Updated Oct. 2006. Retrieved on

April 30, 2010. http://www.troubleshooters.com/codecorn/ruby/basictutorial.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_language).

Slagell, Mark. “What Is Ruby?” Updated 2009. Retrieved May 5, 2010. http://www.rubyist.net/~slagell/ruby/index.html