Introduction to Ruby

  • 913 views
Uploaded on

This is a presentation I use for my introduction to the Ruby programming language training sessions. It covers the basics of the language, such as: syntax, variables, classes, instantiating objects, …

This is a presentation I use for my introduction to the Ruby programming language training sessions. It covers the basics of the language, such as: syntax, variables, classes, instantiating objects, and much more.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
913
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Intro to RubyBy Mark MenardEnable Labs
  • 2. What is a dynamic language? “…a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution.” -Dynamic Programming Languages from Wikipedia
  • 3. Examples of Dynamic LanguagesLispSchemeSmalltalkPerlLuaPHPJavascriptGroovyRubyPython 3
  • 4. Characteristics of Dynamic LanguagesSupport duck typing.Object oriented ones commonly have open classes.Usually have a terse low ceremony syntax.Easily support scripting.Commonly support a REPL for interactive usage.Some support meta-programming - “Die boiler plate die!” Runtime eval Runtime method definition Macros (ala Lisp)Strong support for reflection.
  • 5. Dynamic vs. StaticDynamic Languages Static LanguagesVariable types do not have to be Variable types generally have to bespecified. specified.Class definitions can be modified at run Class definitions are frozen at time oftime. specification.Standard library can be augmented. Standard library is frozen.Generally terse. Generally more verbose.Frequently used for scripting. Generally not used for scripting.Usually aren’t compiled. Generally are compiled.Commonly run on a Virtual Machine. Can run on a VM or on bare metal.Some are interpreted.Typically support reflection. Can support reflection.
  • 6. Intro to RubyDynamic Object Oriented Mixins Language Everything is an expression Everything is an object ClosuresDuck typed Literal arrays, hashes andHas open classes regexesUses dynamic method Operator overloading dispatch Runs on Windows, Linux,Supports method_missing *nix, OS X, Java, and .Net functionalitySupport meta-programmingExecutable Class DefinitionsREPL
  • 7. ScriptingMany dynamic languages are easily at home scripting.Allows easy proof of concept before committing to a large project.Can be used for many system administration needs. Full power of a multi-purpose language. 7
  • 8. Terse / Low CeremonyRuby uses unchecked exceptions. No more try…catch…finally blocks everyplace. No more throws statements.Cleaner syntax.No type declarations.See the solution not the noise. 8
  • 9. REPL: irbAccess to the entire language and functionality in an interactive environment.Prototype new functionality.Explore functionality of new libraries.Bug test problems in your code.Interact with your application while it is running. 9
  • 10. VariablesDo not need to be declared, just use it and it springs into existence. Instance variables start with @ (ie: @name) M ethod variables are just the name (ie: name = ) Class variables start with @@ (ie: @@class_var = 1) 10
  • 11. Literal Arrays, Hashes and Regex in Ruby# Create an arrayarray = [ 1, 2, 3, 4, 5 ]puts array.inspect# Create a hashhash = { :a => a, :b => b, 1 => 1, "a" => "string a" }puts hash.inspectstring = "abcd"puts string =~ /abcd/ ? ”match" : ”no match" #=> ”match"puts string =~ /dcba/ ? ”match" : ”no match" #=> ”no match"
  • 12. Classes in Rubyclass Foo def do_something puts "Foo#do_something" endendclass Bar < Foo def do_something puts "Bar#do_something" endendfoo = Foo.newfoo.do_something #=> prints Foo#do_somethingbar = Bar.newbar.do_something #=> prints Bar#do_something
  • 13. Open ClassesClasses are open for modification at runtime. Methods can be added. Methods can be redefined. Methods can be added to an instance. Methods can be redefined on an instance.Allows you to closely adapt the language to your problem domain. It’s like sculpting in clay instead of stone.Mocking and stubbing become trivial. 13
  • 14. Open Classesclass String def url_decode CGI::unescape(self) end def url_encode CGI::escape(self) endend
  • 15. Open Classes in Rubyclass Foo def escape puts "Whee! Im free!" endendfoo = Foo.newfoo.escape #=> prints "Whee! Im free!"class Foo def escape puts "Not so fast!" endendfoo.escape #=> prints ”Not so fast!"
  • 16. Mixins in Rubymodule Logging def log (msg) puts msg endendclass OrderDispatcher include Logging def dispatch_international_order (order) destination = DistinationService.find(order.foreign_party) log("Sending #{order.number} to #{destination.name}") ... endend
  • 17. Duck Typing“If it walks like a duck, quacks like a duck and has feathers then it’s a duck.”Interfaces are conceptual, not actual.An object’s type is defined by the messages it responds to.
  • 18. Duck Typing in Rubyclass Cat def talk def talk puts "Hi" puts "Meow" end end endend [ Cat.new, Dog.new, Duck.new,class Dog Person.new ].each do |ob| def talk ob.talk puts "Woof" end endendclass Duck def talk puts "Quack!" endendclass Person
  • 19. Closures in Rubydef create_closure (name) # Create a closure closing over the scope which contains name lambda do |job| puts "#{name} has a new job doing #{job}." endendclosure = create_closure("Mark")closure.call("web development") #=> Mark has a new job doing web development.closure.call("goat milking") #=> Mark has a new job doing goat milking.
  • 20. Method Missing in Rubyclass MethodMissing def method_missing (name, *args) puts "Oops! Method #{name} does not exist." endendmm = MethodMissing.newmm.foo #=> prints “Oops! Method foo does not exist.
  • 21. Metaprogramming in Rubyclass MethodMissing def method_missing (name, *args) puts "method_missing called the first time." puts "Defining #{name} method." instance_eval %Q{ def #{name.to_s} (args) puts "Inside the dynamically defined foo method." end } send(name, args) endendmm = MethodMissing.newmm.foo(nil)mm.foo(nil)
  • 22. Operator Overloading in Rubyclass Person def initialize (name) @name = name end def + (other) "#{@name} and #{other.to_s} have gotten together" end def to_s @name endendmark = Person.new("Mark")sylva = Person.new("Sylva")puts mark + sylva #=> "Mark and Sylva have gotten together"