740 likes | 947 Views
Ruby on Rails. Barry Burd Drew University Barry@Burd.org Copy of these slides: http://www.burd.org/RoR.ppt. Agenda. Ruby language Creating a Web application with Ruby on Rails Fine tuning the Web application Doing other neat things. What is …?. Ruby
E N D
Ruby on Rails Barry Burd Drew University Barry@Burd.org Copy of these slides: http://www.burd.org/RoR.ppt
Agenda • Ruby language • Creating a Web application with Ruby on Rails • Fine tuning the Web application • Doing other neat things
What is …? • Ruby • A simple, elegant, loosely typed, reflective, interpreted programming language. • Rails • An add-on to Ruby for web/database handling. • Model-View-Controller • Convention over configuration • DRY philosophy
Ruby on Rails • Advantage • Lightning-fast prototyping • Disadvantages(?) • Development is not as fast with legacy db • Development is not as fast if you customize • Ruby programmers use Ruby idioms • Difficult to deploy • Security model isn’t as mature as Java EE
Hello world program C:\ruby>irb --prompt simple >> puts "Hello" Hello => nil >> exit C:\ruby>
Defining and calling a method C:\ruby>irb --prompt simple >> def show(what) >> "You typed " + what + "." >> end => nil >> show("Hello") => "You typed Hello.“ >> puts show("Hello") You typed Hello. => nil
Expression interpolation >> def show(what) >> "You typed #{what}." >> end => nil >> puts show("Hello") You typed Hello. => nil >> def show(what) >> 'You typed #{what}.' >> end => nil >> puts show("Hello") You typed #{what}. => nil
Ruby is dynamically typed(“duck typing”) >> i = 7 => 7 >> puts i, "\n" 7 => nil >> i = "Hello" => "Hello" >> puts i Hello => nil
Regular Expressions >> line = "Perl" => "Perl" >> if line =~ /Perl|Python/ >> puts "Scripting language mentioned: #{line}" >> end Scripting language mentioned: Perl => nil >> line = "Java" => "Java" >> if line =~ /Perl|Python/ >> puts "Scripting language mentioned: #{line}" >> end => nil
Blocks >> def call_block >> puts "1" >> yield >> yield >> puts "3" >> end => nil >> call_block {puts "2"} 1 2 2 3 => nil
Blocks with parameters >> def call_block >> puts "1" >> yield(42) >> yield(79) >> puts "3" >> end => nil >> call_block {|x| puts x} 1 42 79 3 => nil
>> 5.times {puts "*"} * * * * * => 5 >> 3.upto(6) {|i| puts i} 3 4 5 6 => 3 >> ('a'..'e').each {|char| puts char} a b c d e => "a".."e"
Another Ruby program C:\>type fact.rb def fact(n) return 1 if n == 0 f = 1 n.downto(1) do |i| f *= i end return f end print fact(ARGV[0].to_i), "\n" C:\>ruby fact.rb 10 3628800 C:\>ruby fact.rb 5 120
Ruby’s integers (and other things) are of arbitrary size C:\>ruby fact.rb 155 4789142901463393876335775239063022722176295591337767174070096339929153381622433264146569329274347655956110484372311586936020749175429076661003216274382475477806479918110524333880196139452687559896255940215628508414806740389616633144934400000000000000000000000000000000000000
Ruby is object-oriented class Point def initialize(x, y) @x = x; @y = y self end def to_s sprintf("%d@%d", @x, @y) end end list1 = [10, 20, Point.new(2, 3), Point.new(4, 5)] list2 = [20, Point.new(4, 5), list1] print("list1: ", list1.inspect, "\n") print("list2: ", list2.inspect, "\n") C:\>ruby list3.rb list1: [10, 20, #<Point:0x2867a48 @y=3, @x=2>, <Point:0x2867a30 @y=5, @x=4>] list2: [20, #<Point:0x28679e8 @y=5, @x=4>, [10, 20, #<Point:0x2867a48 @y=3, @x=2>, #<Point:0x2867a30 @y=5, @x=4>]]
Ruby has unit testing class Adder def initialize(number) @number = number end def add(number) return @number + number end end require 'test/unit' require 'adder' class TC_Adder < Test::Unit::TestCase def setup @adder = Adder.new(5) end def test_add assert_equal(7, @adder.add(2), "Should have added correctly") end def teardown @adder = nil end end
Class with a constructor(from “Programming Ruby”) class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end end song = Song.new("Bicylops","Fleck",260)
Getters # This code can come immediately after the previous # slide’s code. class Song def name @name end def artist @artist end def duration @duration end end puts song.name
Quick way to create getters class Song attr_reader :name, :artist, :duration end puts song.name
>> class MyClass >> end => nil >> MyClass.methods => ["send", "name", "class_eval", "object_id", "new", "singleton_methods", "__se nd__", "private_method_defined?", "equal?", "taint", "frozen?", "instance_variab le_get", "constants", "kind_of?", "to_a", "instance_eval", "require", "ancestors ", "const_missing", "type", "instance_methods", "protected_methods", "extend", " protected_method_defined?", "eql?", "public_class_method", "const_get", "instanc e_variable_set", "hash", "is_a?", "autoload", "to_s", "class_variables", "class" , "tainted?", "private_methods", "public_instance_methods", "instance_method", " require_gem_with_options", "untaint", "included_modules", "private_class_method" , "const_set", "id", "<", "inspect", "<=>", "==", "method_defined?", ">", "===", "clone", "public_methods", "protected_instance_methods", ">=", "respond_to?", " display", "freeze", "<=", "module_eval", "autoload?", "allocate", "__id__", "=~" , "methods", "require_gem", "method", "public_method_defined?", "superclass", "n il?", "dup", "private_instance_methods", "instance_variables", "include?", "call _block", "const_defined?", "instance_of?"]
Ruby is Reflective (1) class Myclass def mymethod puts "Hello" end end x = 'mymethod' Myclass.new.send(x) Hello
Ruby is Reflective (2) class MyStuff def announce puts "I'm being announced.\n" end def method_missing(method_name) puts "I don't know how to do #{method_name}.\n" end end m = MyStuff.new m.announce m.xyz I'm being announced. I don't know how to do xyz.
Creating a web-based database application in about a dozen steps…
Rails naming conventions • company application • company_development database • employees table • generate model Employee • generate controller Employee • employee_controller.rb • EmployeeController class