• Like
Ruby
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

Published in Business
  • 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
1,477
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
40
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
  • \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

Transcript

  • 1. Ruby Masterclass Kerry Buckley 16 June 2011
  • 2. History• Created in 1993• First public release Christmas 1995• ‘Pickaxe’ book first published in 2001• Ruby on Rails released in 2005
  • 3. 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
  • 4. 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
  • 5. Characteristics• Strongly but dynamically typed• Interpreted• Object-oriented• …but can be procedural• …or even (mostly) functional• Concise but readable
  • 6. Applications• Web apps• Shell scripting• Build automation• Deployment automation• ‘Glue’ code
  • 7. Culture• Permissive open source (MIT)• Favour simplicity• Agile (especially TDD)• Mac/Linux• Fast-moving• Fun!
  • 8. Basic syntax
  • 9. puts "hello world"
  • 10. Variables$global_variable = 123@@class_variable = "hello"@instance_variable = [1, 2, 3]local_variable = true
  • 11. Built-in types421.5"string":symbol[1, 2, "foo"]{1 => "one", 2 => "two"}(1..10)/^foo.*$/truefalsenil
  • 12. 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"
  • 13. Accessors
  • 14. Accessorsclass Rectangle attr_accessor :x attr_reader :y def initialize x, y @x, @y = x, y endend
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. if, else and unless
  • 19. if, else and unlessif a == b puts "The same"elsif a < b puts "A is lower"else puts "A is higher"end
  • 20. 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
  • 21. 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?
  • 22. 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
  • 23. Loopsfor a in (1..10) do puts aendb=0while b <= 10 puts b b += 1end
  • 24. Including other code• load• require• Rubygems
  • 25. Exercise 1 Playing with irb
  • 26. Everything is an object42.class #=> FixnumFixnum.class #=> Classnil.class #=> NilClasstrue.class #=> TrueClassmethod(:puts).class #=> Method
  • 27. 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
  • 28. Dynamic typingfoo = 123foo.class #=> Fixnumfoo = "foo"foo.class #=> String
  • 29. Open classesputs 123 #=> "123"class Fixnum def to_s "Not telling you!" endendputs 123 #=> "Not telling you!"
  • 30. 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.
  • 31. 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"
  • 32. Blocks
  • 33. Blocksbegin puts "This is a block, but its not much use"end
  • 34. 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
  • 35. 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
  • 36. 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" }
  • 37. Blocks with argumentsblock = Proc.new do |arg| puts "You called me with #{arg.inspect}"endblock.call("foo") #=> You called me with "foo"
  • 38. 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.
  • 39. 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.
  • 40. File IO with blocks
  • 41. File IO with blocksfile = File.open "logfile", "a"file.puts "log message"file.close
  • 42. File IO with blocksfile = File.open "logfile", "a"file.puts "log message"file.closeFile.open "logfile", "a" do |file| file.puts "log message"end
  • 43. Modules
  • 44. Modulesmodule InfoDisplay def display_info puts "Class: #{self.class}, ID: #{object_id}" endendclass SomeClass include InfoDisplayendSomeClass.new.display_info #=> "Class: SomeClass, ID: 2159853660"
  • 45. 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"
  • 46. 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"]
  • 47. Exercise 2Working with Enumerable
  • 48. Metaprogramming• Programs writing programs• Modify behaviour at runtime• Generate code• Respond dynamically to messages
  • 49. Evaluating code
  • 50. Evaluating codecode = puts "hello"eval code #=> "hello"
  • 51. Evaluating codecode = puts "hello"eval code #=> "hello"foo = "Ruby"foo.instance_eval do def hello puts "Hello, Im #{self}" endendfoo.hello #=> "Hello, Im Ruby"
  • 52. Dynamic calling
  • 53. Dynamic callingfoo = "Hello"foo.upcase #=> "HELLO"foo.send :upcase #=> "HELLO"
  • 54. Dynamic callingfoo = "Hello"foo.upcase #=> "HELLO"foo.send :upcase #=> "HELLO"method = [:upcase, :downcase][rand(2)]foo.send method #=> Randomly "HELLO" or "hello"
  • 55. 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
  • 56. 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"]
  • 57. Exercise 3 Metaprogramming
  • 58. 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]
  • 59. Exercise 4 Rake
  • 60. Web frameworks and many more…
  • 61. Web serversMongrel CGI Webrick
  • 62. Before Rack
  • 63. Before Rack
  • 64. Before Rack
  • 65. Before Rack
  • 66. Before Rack
  • 67. With RackRack-compliant interface
  • 68. A rack apprequire "rack"class HelloWorld def call env [200, {"Content-Type" => "text/plain"}, StringIO.new("Hello world")] endendrun HelloWorld.new
  • 69. Exercise 5 Rack
  • 70. 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
  • 71. Testing• Test/Unit & SimpleTest• RSpec• Cucumber• Capybara
  • 72. JRuby• Another implementation of Ruby• Written in Java• Comparable or better performance• Use Java libraries from Ruby code• Deploy Rails apps as WAR files
  • 73. 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
  • 74. 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(); }}
  • 75. while Time.now.hour < 17 audience_suggestions.pop.discussendgo :home