Ruby
Upcoming SlideShare
Loading in...5
×
 

Ruby

on

  • 1,704 views

 

Statistics

Views

Total Views
1,704
Views on SlideShare
1,704
Embed Views
0

Actions

Likes
1
Downloads
36
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

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
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Ruby Ruby Presentation Transcript

  • Ruby Masterclass Kerry Buckley 16 June 2011
  • History• Created in 1993• First public release Christmas 1995• ‘Pickaxe’ book first published in 2001• Ruby on Rails released in 2005
  • Philosophy“I wanted a scripting language that wasmore powerful than Perl, and more object-oriented than Python. That’s why I decidedto design my own language” — Yukihiro ‘Matz’ Matsumoto
  • Philosophy“I hope to see Ruby help everyprogrammer in the world to be productive,and to enjoy programming, and to behappy. That is the primary purpose ofRuby language.” — Yukihiro ‘Matz’ Matsumoto
  • Characteristics• Strongly but dynamically typed• Interpreted• Object-oriented• …but can be procedural• …or even (mostly) functional• Concise but readable
  • Applications• Web apps• Shell scripting• Build automation• Deployment automation• ‘Glue’ code
  • Culture• Permissive open source (MIT)• Favour simplicity• Agile (especially TDD)• Mac/Linux• Fast-moving• Fun!
  • Basic syntax
  • puts "hello world"
  • Variables$global_variable = 123@@class_variable = "hello"@instance_variable = [1, 2, 3]local_variable = true
  • Built-in types421.5"string":symbol[1, 2, "foo"]{1 => "one", 2 => "two"}(1..10)/^foo.*$/truefalsenil
  • Classes & methodsclass MyClass def initialize number @number = number end def double @number * 2 end def hello(name = "world") puts "Hello #{name.capitalize}" endendmy_object = MyClass.new(2)puts my_object.double #=> prints "4"my_object.hello #=> prints "hello World"my_object.hello "kerry" #=> prints "hello Kerry"
  • Accessors
  • Accessorsclass Rectangle attr_accessor :x attr_reader :y def initialize x, y @x, @y = x, y endend
  • Accessorsclass Rectangle attr_accessor :x attr_reader :y def initialize x, y @x, @y = x, y endendrect = Rectangle.new 2, 4rect.x #=> 2rect.y #=> 4
  • Accessorsclass Rectangle attr_accessor :x attr_reader :y def initialize x, y @x, @y = x, y endendrect = Rectangle.new 2, 4rect.x #=> 2rect.y #=> 4rect.x = 3rect.x #=> 3
  • Accessorsclass Rectangle attr_accessor :x attr_reader :y def initialize x, y @x, @y = x, y endendrect = Rectangle.new 2, 4rect.x #=> 2rect.y #=> 4rect.x = 3rect.x #=> 3rect.y = 3 #=> NoMethodError
  • if, else and unless
  • if, else and unlessif a == b puts "The same"elsif a < b puts "A is lower"else puts "A is higher"end
  • if, else and unlessif a == b puts "The same"elsif a < b puts "A is lower"else puts "A is higher"endunless finished? do_stuffend
  • if, else and unlessif a == b puts "The same"elsif a < b puts "A is lower"else puts "A is higher"endunless finished? do_stuffendraise "Oops" if result.nil?
  • casecase foowhen nil puts "foo is nil"when /^a/i puts "foo begins with an A"when String puts "foo is a string"when (1..10) puts "foo is in the range 1-10"else puts "foo is something else"end
  • Loopsfor a in (1..10) do puts aendb=0while b <= 10 puts b b += 1end
  • Including other code• load• require• Rubygems
  • Exercise 1 Playing with irb
  • Everything is an object42.class #=> FixnumFixnum.class #=> Classnil.class #=> NilClasstrue.class #=> TrueClassmethod(:puts).class #=> Method
  • Duck typingclass Duck def quack "Quack" endendclass Dog def quack "Woof" endendfor animal in [Duck.new, Dog.new] do puts animal.quackend"a string".quack #=> NoMethodError
  • Dynamic typingfoo = 123foo.class #=> Fixnumfoo = "foo"foo.class #=> String
  • Open classesputs 123 #=> "123"class Fixnum def to_s "Not telling you!" endendputs 123 #=> "Not telling you!"
  • Open classesputs 123 #=> "123"class Fixnum def to_s "Not telling you!" endendputs 123 #=> "Not telling you!" You probably don’t want to do this.
  • Aliasing methodsrequire "fileutils"class << FileUtils def rm_with_prompt *files print "Are you sure you want to delete #{files}? " response = gets.chomp.downcase return unless %w{yes y}.include?(response) rm_without_prompt *files end alias_method :rm_without_prompt, :rm alias_method :rm, :rm_with_promptendFileUtils.rm "a_file"
  • Blocks
  • Blocksbegin puts "This is a block, but its not much use"end
  • Blocksbegin puts "This is a block, but its not much use"endblock = Proc.new do puts "Now Im assigned to a variable"end# Nothing printed yet
  • Blocksbegin puts "This is a block, but its not much use"endblock = Proc.new do puts "Now Im assigned to a variable"end# Nothing printed yetblock.call #=> executes the block
  • Blocksbegin puts "This is a block, but its not much use"endblock = Proc.new do puts "Now Im assigned to a variable"end# Nothing printed yetblock.call #=> executes the blockProc.new { puts "curly brackets work too" }
  • Blocks with argumentsblock = Proc.new do |arg| puts "You called me with #{arg.inspect}"endblock.call("foo") #=> You called me with "foo"
  • Explicit block argsdef n_times(n, &block) puts "starting..." n.times { block.call } puts "done."endn_times(3) do puts "hello"end#=> starting...#=> hello#=> hello#=> hello#=> done.
  • Implicit block argsdef n_times(n) puts "starting..." n.times { yield } puts "done."endn_times(3) do puts "hello"end#=> starting...#=> hello#=> hello#=> hello#=> done.
  • File IO with blocks
  • File IO with blocksfile = File.open "logfile", "a"file.puts "log message"file.close
  • File IO with blocksfile = File.open "logfile", "a"file.puts "log message"file.closeFile.open "logfile", "a" do |file| file.puts "log message"end
  • Modules
  • Modulesmodule InfoDisplay def display_info puts "Class: #{self.class}, ID: #{object_id}" endendclass SomeClass include InfoDisplayendSomeClass.new.display_info #=> "Class: SomeClass, ID: 2159853660"
  • Modulesmodule InfoDisplay def display_info puts "Class: #{self.class}, ID: #{object_id}" endendclass SomeClass include InfoDisplayendSomeClass.new.display_info #=> "Class: SomeClass, ID: 2159853660"String.send :include, InfoDisplay"foo".display_info #=> "Class: String, ID: 2159853520"
  • Modules: Enumerableclass Words include Enumerable def initialize string @words = string.split end def each @words.each {|word| yield word } endendwords = Words.new("The quick brown fox")puts words.first #=> "The"puts words.count #=> 4p words.select{|w| w.length == 3 } # => ["The", "fox"]
  • Exercise 2Working with Enumerable
  • Metaprogramming• Programs writing programs• Modify behaviour at runtime• Generate code• Respond dynamically to messages
  • Evaluating code
  • Evaluating codecode = puts "hello"eval code #=> "hello"
  • Evaluating codecode = puts "hello"eval code #=> "hello"foo = "Ruby"foo.instance_eval do def hello puts "Hello, Im #{self}" endendfoo.hello #=> "Hello, Im Ruby"
  • Dynamic calling
  • Dynamic callingfoo = "Hello"foo.upcase #=> "HELLO"foo.send :upcase #=> "HELLO"
  • Dynamic callingfoo = "Hello"foo.upcase #=> "HELLO"foo.send :upcase #=> "HELLO"method = [:upcase, :downcase][rand(2)]foo.send method #=> Randomly "HELLO" or "hello"
  • Dynamic callingfoo = "Hello"foo.upcase #=> "HELLO"foo.send :upcase #=> "HELLO"method = [:upcase, :downcase][rand(2)]foo.send method #=> Randomly "HELLO" or "hello"bar = 2bar.send :+, 2 #=> 4
  • Dynamic responseclass Foo def method_missing name, *args puts "You called #{name} with #{args.inspect}" endendFoo.new.bar(1, "hello") #=> You called bar with [1, "hello"]
  • Exercise 3 Metaprogramming
  • Rakerequire "erb"require "rake/clean"CLOBBER.include("output/*")OUTPUT_FILE = "output/README.txt"TEMPLATE_FILE = "templates/README.txt.erb"desc "Build the README file from the template"file OUTPUT_FILE => TEMPLATE_FILE do template = File.read(TEMPLATE_FILE) File.open(OUTPUT_FILE, "w") do |f| output = ERB.new(template).result(binding) f.write output endendtask :default => [OUTPUT_FILE]
  • Exercise 4 Rake
  • Web frameworks and many more…
  • Web serversMongrel CGI Webrick
  • Before Rack
  • Before Rack
  • Before Rack
  • Before Rack
  • Before Rack
  • With RackRack-compliant interface
  • A rack apprequire "rack"class HelloWorld def call env [200, {"Content-Type" => "text/plain"}, StringIO.new("Hello world")] endendrun HelloWorld.new
  • Exercise 5 Rack
  • Shell scripting#!/usr/bin/env rubyrequire "fileutils"include FileUtilsARGV.each do |file| cp file, "#{file}.bak"endsystem "service httpd restart" #=> returns true or falseusers = `users`.split
  • Testing• Test/Unit & SimpleTest• RSpec• Cucumber• Capybara
  • JRuby• Another implementation of Ruby• Written in Java• Comparable or better performance• Use Java libraries from Ruby code• Deploy Rails apps as WAR files
  • Calling Java from Rubyrequire javaframe = javax.swing.JFrame.new "Window"label = javax.swing.JLabel.new "Hello"frame.getContentPane.add labelframe.setDefaultCloseOperation( javax.swing.JFrame::EXIT_ON_CLOSE)frame.packframe.setVisible true
  • Calling Ruby from Javaimport org.jruby.embed.ScriptingContainer;public class HelloWorld { private HelloWorld() { ScriptingContainer c = new ScriptingContainer(); c.runScriptlet("puts "Hello World!""); } public static void main(String[] args) { new HelloWorld(); }}
  • while Time.now.hour < 17 audience_suggestions.pop.discussendgo :home