ruby in 20 minutes

21
Ruby in 20 minutes John Pinney [email protected]

Upload: heman

Post on 24-Feb-2016

58 views

Category:

Documents


0 download

DESCRIPTION

Ruby in 20 minutes. John Pinney [email protected]. What is Ruby?. A true object-oriented language with easily understandable syntax and convenient notations. => pure / elegant / powerful - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Ruby in 20 minutes

Ruby in 20 minutes

John [email protected]

Page 2: Ruby in 20 minutes

What is Ruby?

A true object-oriented languagewith easily understandable syntaxand convenient notations.

=> pure / elegant / powerful

“I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.”

Yukihiro Matsumoto (“Matz”)

Page 3: Ruby in 20 minutes

The Ruby interpreter

> irb

puts "Hello World"Hello World

Page 4: Ruby in 20 minutes

(Almost) everything is an object

There are no primitives in Ruby.

Nearly everything can be considered as an object, i.e.is an instance of a classhas an interface for method invocation

Page 5: Ruby in 20 minutes

"hello".reverse=> "olleh”

44.even?=> true

nil.class=> NilClass

nil.class.class=> Class

Page 6: Ruby in 20 minutes

Parts of speech

VariablesNumbersStringsSymbolsConstants

RangesRegular Expressions

Methods + argumentsBlocks + arguments

ArraysHashes

Operators

Keywords

Page 7: Ruby in 20 minutes

Variables

local_variable = 3.0@instance_variable = 4.6@@class_variable = 8.9

(@ = “attribute”)

$global_variable = 2.5

Page 8: Ruby in 20 minutes

Numbers

1.class => Fixnum

1.0.class=> Float

Page 9: Ruby in 20 minutes

Symbols

Symbols (starting with a colon ) are like lightweight strings.They always point to the same object, wherever used in the code.

x = :my_symbol

Page 10: Ruby in 20 minutes

Methods

front_door.openfront_door.open.closefront_door.is_open?front_door.paint(3,:red)front_door.paint(3,:red).dry(30).close

Page 11: Ruby in 20 minutes

Kernel methods are invoked by their names alone.This is just a bit of syntactic sugar.

print "Hello\n"Hello

Kernel.print("Hello\n")Hello

(print is really a class method of Kernel)

Page 12: Ruby in 20 minutes

Operators behave as you would expect:1 + 23

But in Ruby they are actually methods!1.+(2)3

Since all operators are methods, they can be defined as you like for your own classes, or even re-defined for existing classes.

Operators

Page 13: Ruby in 20 minutes

BlocksAny code surrounded by curly braces is a closure, known as a block:

20.times{ print 'CAG'}CAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAG

Blocks are sets of instructions that can be passed around the program.

Page 14: Ruby in 20 minutes

Blocks can also take arguments, delimited by pipe characters:

"Hello".each_char{|ch| puts ch}Hello

Page 15: Ruby in 20 minutes

Braces can be replaced by the keywords do and end.

"Hello".each_char do |ch| puts chendHello

Page 16: Ruby in 20 minutes

Effective use of blocks can allow highly transparent code:

a = [ "a", "b", "c", "d" ]a.collect { |x| x + "!" } ["a!", "b!", "c!", "d!"]

Page 17: Ruby in 20 minutes

Regular expressionsRegex is very simple to use in Ruby (much nicer than Python!)

text = "Cats are smarter than dogs”;if ( text =~ /(C|c)at(.*)/ ) then puts "I found a cat"endI found a cat

Page 18: Ruby in 20 minutes

Example

class Greeter def initialize(name = "World") @name = name end def say_hi puts "Hi #{@name}!" end def say_bye puts "Bye #{@name}, come back soon." endend

Page 19: Ruby in 20 minutes

g = Greeter.new("Pat")g.say_hiHi Pat!g.say_byeBye Pat, come back soon.

g.nameNoMethodError: undefined method 'name' for #<Greeter:0x007f91b18708f8 @name="Pat">

Page 20: Ruby in 20 minutes

class Greeter attr_accessor :nameendg.name=> "Pat"

g.name = "John"g.say_hiHi John!

Page 21: Ruby in 20 minutes

Sources:

https://www.ruby-lang.org/en/documentation/quickstart/

http://docs.ruby-doc.com/docs/ProgrammingRuby/

http://mislav.uniqpath.com/poignant-guide/

http://tryruby.org/