Download presentation
Presentation is loading. Please wait.
Published byCharlene Parrish Modified over 6 years ago
1
6 - Ruby in the Rough 7 - Ruby on Rails 03/03/2006 Taylan Yemliha
Beyond Java 6 - Ruby in the Rough 7 - Ruby on Rails 03/03/ Taylan Yemliha
2
Ruby Dynamic Fully object-oriented Interpreted 03/03/2006
3
OO Everything is an object (ints, strings, etc.) irb(main):002:0> 7
=> 7 irb(main):003:0> 7.class Fixnum irb(main):011:0> 7.5.round => 8 irb(main):012:0> nil.class => NilClass 03/03/2006
4
Typing Dynamically typed irb(main):013:0> n=1 => 1
irb(main):014:0> n.class => Fixnum irb(main):015:0> n="bbs" => "bbs" irb(main):016:0> n.class => String 03/03/2006
5
Typing Strongly typed irb(main):017:0> n+3
TypeError: cannot convert Fixnum into String from (irb):17:in `+' from (irb):17 03/03/2006
6
Conditionals irb(main):020:0> def silence?(b)
irb(main):021:1> puts "aaaa" if b irb(main):022:1> end => nil irb(main):023:0> silence? true aaaa irb(main):025:0> silence? false irb(main):026:0> silence? 0 irb(main):028:0> silence? nil 03/03/2006
7
Looping irb(main):030:0> puts line while line = gets "aaa" one two
three ^Z => nil 03/03/2006
8
Ranges irb(main):031:0> range = 1..3 => 1..3
irb(main):032:0> range.class => Range irb(main):033:0> ('a'..'z')==='h' => true irb(main):034:0> ('a'..'z')==='H' => false irb(main):035:0> (1..10) === 5 03/03/2006
9
Ranges irb(main):036:0> for c in 'g'.. 'j'
irb(main):037:1> print c + '-' irb(main):038:1> end g-h-i-j-=> "g".."j" irb(main):039:0> for c in 'g'.. 'j' irb(main):040:1> puts c irb(main):041:1> end g h i j => "g".."j" 03/03/2006
10
Regular Expressions irb(main):042:0> regex = /better/
irb(main):043:0> regex.class => Regexp irb(main):044:0> "Mine is bigger" =~ regex => nil irb(main):045:0> "Mine is better" =~ regex => 8 03/03/2006
11
Containers - Hashes irb(main):046:0> numbers={0=>"zero", 1=>"one", 2=>"two", 3=>"three"} => {0=>"zero", 1=>"one", 2=>"two", 3=>"three"} irb(main):047:0> 4.times {|i| puts numbers[i]} zero one two three => 4 03/03/2006
12
Containers - Arrays irb(main):048:0> stack = [1,2,3]
=> [1, 2, 3] irb(main):049:0> stack.push "cat" => [1, 2, 3, "cat"] irb(main):050:0> stack.pop => "cat" irb(main):051:0> stack 03/03/2006
13
Files A simple GREP ruby grep.rb filename regex
File.open(ARGV[0]) do |file| rx = Regexp.new(ARGV[1]) while line=file.gets puts line if line =~ rx end ruby grep.rb filename regex 03/03/2006
14
Applying Some Structure
Inside-out Refactoring Group common code in libraries Call them in your methods Outside in refactoring Your methods can take code-blocks as parameters call them even pass parameters to them 03/03/2006
15
Outside-in Refactoring
def threeTimes yield end threeTimes { puts "Hello" } produces: Hello 03/03/2006
16
Classes calculator.rb class Calculator def initialize @total = 0 end
def add(x) @total += x def subtract(x) @total -= x 03/03/2006
17
Classes irb(main):001:0> require calculator
NameError: undefined local variable or method `calculator' for main:Object from (irb):1 irb(main):002:0> require 'Calculator' => true irb(main):003:0> c = Calculator.new => irb(main):004:0> c.add 100 => 100 irb(main):005:0> c.subtract 40 => 60 03/03/2006
18
Classes irb(main):006:0> class Calculator
irb(main):007:1> def reset = 0 irb(main):009:2> end irb(main):010:1> end => nil irb(main):011:0> c.reset => 0 03/03/2006
19
Classes irb(main):012:0> class IrsCalculator < Calculator
irb(main):013:1> def add(x) irb(main):014:2> x = x/2 if x>0 irb(main):015:2> super irb(main):016:2> end irb(main):017:1> end => nil irb(main):018:0> c = IrsCalculator.new => irb(main):019:0> c.add 100 => 50 03/03/2006
20
Classes irb(main):020:0> Class.superclass => Module
irb(main):021:0> Module.superclass => Object irb(main):022:0> Object.superclass => nil 03/03/2006
21
Mixins Interfaces with implementation
Can access the including class’s methods Partly adds the benefit of multi-inheritance Implemented using Modules 03/03/2006
22
Interceptors You can rename methods on-the-fly class Class
alias_method :orig_new, :new def new(*args) result = orig_new(*args) print “Unattended laptop error! ” #puts result return result end 03/03/2006
23
Interceptors irb(main):001:0> require "Class" => true
irb(main):002:0> a=[1,2,3] #<RubyToken::TkIDENTIFIER:0x2941d40> #<RubyToken::TkASSIGN:0x2941b48> #<RubyToken::TkLBRACK:0x > #<RubyToken::TkINTEGER:0x29416c8> #<RubyToken::TkCOMMA:0x29414d0> #<RubyToken::TkINTEGER:0x > #<RubyToken::TkCOMMA:0x29410b0> #<RubyToken::TkINTEGER:0x2940e70> #<RubyToken::TkRBRACK:0x2940c60> #<RubyToken::TkNL:0x > => [1, 2, 3] 03/03/2006
24
Ruby On Rails What is it? “Rails is a full-stack, open-source web framework in Ruby for writing real-world applications with joy and less code than most frameworks spend doing XML sit-ups” David H. Hansson 03/03/2006
25
Some Numbers The author’s test case gives shows impressively superior numbers for Rails over Java/Spring/Hibernate solution in development time/code size as well as performance in most cases. 03/03/2006
26
Architecture 03/03/2006
27
Under the Hood Active Record It’s a wrapper around a db table, with domain logic built into the wrapper. You can also do inheritance and manage relationships. Action Pack Splits the request into a controller part and a view part. 03/03/2006
28
Showcases RubyBB by Russ Smith http://rubybb.readbim.com/
384 Lines of Code StoryCards Web app to support XP style dev. by Jim Weirich 1,250 Lines of code 8 hours of development time Basecamp: A commercial Rails web-app with over 10,000 users. “Web-based project management” Launched after 4,000 Lines of Code. 2 man-months of months of programming by a single developer 03/03/2006
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.