6 Ruby in the Rough 7 Ruby on Rails

tunisianbromidrosisInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 8 μήνες)

109 εμφανίσεις

6
-

Ruby in the Rough

7
-

Ruby on Rails



03/03/2006 Taylan Yemliha

Beyond Java

03/03/2006

Ruby


Dynamic


Fully object
-
oriented


Interpreted

03/03/2006

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

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

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

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

=> nil

irb(main):025:0> silence? false

=> nil

irb(main):026:0> silence? 0

aaaa

=> nil

irb(main):028:0> silence? nil

=> nil


03/03/2006

Looping

irb(main):030:0> puts line while line = gets

"aaa"

"aaa"

one

one

two

two

three

three

^Z

=> nil

03/03/2006

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

=> true

03/03/2006

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

Regular Expressions

irb(main):042:0> regex = /better/

=> /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

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

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

=> [1, 2, 3]

03/03/2006

Files


A simple GREP

File.open(ARGV[0]) do |file|


rx = Regexp.new(ARGV[1])


while line=file.gets


puts line if line =~ rx


end

end


ruby grep.rb filename regex

03/03/2006

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

Outside
-
in Refactoring

def

threeTimes



yield



yield



yield

end

threeTimes

{

puts

"Hello"

}

produces:


Hello


Hello


Hello

03/03/2006

Classes

calculator.rb

class Calculator


def initialize


@total = 0


end



def add(x)


@total += x


end



def subtract(x)


@total
-
= x


end



end

03/03/2006

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

=> #<Calculator:0x2bd2500 @total=0>

irb(main):004:0> c.add 100

=> 100

irb(main):005:0> c.subtract 40

=> 60

03/03/2006

Classes

irb(main):006:0> class Calculator

irb(main):007:1> def reset

irb(main):008:2> @total = 0

irb(main):009:2> end

irb(main):010:1> end

=> nil

irb(main):011:0> c.reset

=> 0


03/03/2006

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

=> #<IrsCalculator:0x2bba038 @total=0>

irb(main):019:0> c.add 100

=> 50


03/03/2006

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

Mixins


Interfaces with implementation


Can access the including class’s
methods


Partly adds the benefit of multi
-
inheritance


Implemented using Modules

03/03/2006

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

end

03/03/2006

Interceptors

irb(main):001:0> require "Class"

=> true

irb(main):002:0> a=[1,2,3]

#<RubyToken::TkIDENTIFIER:0x2941d40>

#<RubyToken::TkASSIGN:0x2941b48>

#<RubyToken::TkLBRACK:0x2941950>

#<RubyToken::TkINTEGER:0x29416c8>

#<RubyToken::TkCOMMA:0x29414d0>

#<RubyToken::TkINTEGER:0x2941278>

#<RubyToken::TkCOMMA:0x29410b0>

#<RubyToken::TkINTEGER:0x2940e70>

#<RubyToken::TkRBRACK:0x2940c60>

#<RubyToken::TkNL:0x2940438>

=> [1, 2, 3]

03/03/2006

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

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

Architecture

03/03/2006

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

Showcases

RubyBB by Russ Smith

http://rubybb.readbim.com/

384 Lines of Code


StoryCards Web app to support XP style dev. by Jim Weirich

http://onestepback.org:3030/

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”

http://www.basecamphq.com/

Launched after 4,000 Lines of Code.

2 man
-
months of months of programming by a single
developer