programming language ruby and the rails framework

43
Ruby programming language and Ruby on Rails framework

Upload: radek-mika

Post on 15-May-2015

3.620 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Programming language Ruby and the Rails framework

Rubyprogramming language

and Ruby on Rails framework

Presenter
Presentation Notes
Programming language Ruby and Ruby on Rails technology - introduction of the language, advantages and disadvantages, comparison with other languages, ...
Page 2: Programming language Ruby and the Rails framework

Presentation Agenda What is Ruby?

About the language

Its history

Principles of language

Code examples

Rails framework

2Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 3: Programming language Ruby and the Rails framework

What is Ruby? Programming language

Interpreted language

Modern language

Object-oriented language

Dynamically typed language

Agile language

3Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 4: Programming language Ruby and the Rails framework

Principles of Ruby

4Radek Mika - Unicorn CollegeJanuary 18, 2010

Presenter
Presentation Notes
The Principle of Least SurpriseThis principle is the supreme design goal of Ruby Makes programmers happy and makes Ruby easy to learn Examples What class is an object?o.class Is it Array#size or Array#length?same method –they’re aliased What are the differences between arrays?diff = ary1 –ary2union = ary1 + ary2 Principle of Least Effort We don’t like to waste time Especially on XML config files, getters, setters, etc… The quicker we program, the more we accomplish Sounds reasonable enough, right? Less code means less bugs
Page 5: Programming language Ruby and the Rails framework

Principles of Ruby

5Radek Mika - Unicorn CollegeJanuary 18, 2010

Presenter
Presentation Notes
The Principle of Least SurpriseThis principle is the supreme design goal of Ruby Makes programmers happy and makes Ruby easy to learn Examples What class is an object?o.class Is it Array#size or Array#length?same method –they’re aliased What are the differences between arrays?diff = ary1 –ary2union = ary1 + ary2 Principle of Least Effort We don’t like to waste time Especially on XML config files, getters, setters, etc… The quicker we program, the more we accomplish Sounds reasonable enough, right? Less code means less bugs
Page 6: Programming language Ruby and the Rails framework

Principles of Ruby

Japanese Design

Focus on human factor

Principle of Least Surprise

Principle of Least Effort

6Radek Mika - Unicorn CollegeJanuary 18, 2010

Presenter
Presentation Notes
The Principle of Least SurpriseThis principle is the supreme design goal of Ruby Makes programmers happy and makes Ruby easy to learn Examples What class is an object?o.class Is it Array#size or Array#length?same method –they’re aliased What are the differences between arrays?diff = ary1 –ary2union = ary1 + ary2 Principle of Least Effort We don’t like to waste time Especially on XML config files, getters, setters, etc… The quicker we program, the more we accomplish Sounds reasonable enough, right? Less code means less bugs
Page 7: Programming language Ruby and the Rails framework

The Principle of Least SurpriseThis principle is the supreme design goal of Ruby It makes programmers happy It makes Ruby easy to learnExamples What class is an object?

o.class Is it Array.size or Array.length?

same method - they are aliased What are the differences between arrays?

Diff = ary1 – ary2Union = ary1 + ary2

7Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 8: Programming language Ruby and the Rails framework

The Principle of Least Effort We do not like to waste time

Especially on XML configuration files, getters, setters, etc.

Syntactic sugar wherever you look

The quicker we program, the more we accomplishSounds reasonable enough, does not it?

Less code means less bugs

8Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 9: Programming language Ruby and the Rails framework

Philosophy No perfect language

Have joy

Computers are my servants, not my masters!

Unchangeable small core (syntax) and extensible class libraries

9Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 10: Programming language Ruby and the Rails framework

The History of Ruby

Created in Japan 10 years ago

Created by Yukihiro Matsumoto (known as Matz)

Inspired by Perl, Python, Lisp and Smalltalk

10Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 11: Programming language Ruby and the Rails framework

Comparison with Python

Interactive prompt (similar) No special line terminator (similar) Everything is an object (similar)

X More speed! (ruby is faster)

11Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 12: Programming language Ruby and the Rails framework

Ruby is Truly Object-Oriented Ruby uses single inheritance

X Mixins and Modules allow you to extend classes

without multiple inheritance

Reflection

Things like ‘=’ and ‘+’ which may appear as operators are actually methods (like Smalltalk)

12Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 13: Programming language Ruby and the Rails framework

…is it FAST?

January 18, 2010 Radek Mika - Unicorn College 13

Well, that’s all nice but…

Page 14: Programming language Ruby and the Rails framework

Merge Sort Algorithm

14Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 15: Programming language Ruby and the Rails framework

Ruby Speed Comparison

15Radek Mika - Unicorn CollegeJanuary 18, 2010

3x faster than PHP

2.5x faster than Perl

2x faster than Python

2x (maybe more) C++SLOWER than

Page 16: Programming language Ruby and the Rails framework

Ruby Speed - WARNING

16Radek Mika - Unicorn CollegeJanuary 18, 2010

Previous results are only informational (only Merge Sort Comparison)

Another comparisons usually have different results

Page 17: Programming language Ruby and the Rails framework

When I should not use Ruby?

If I need highly effective and powerful language, e.g. for distributed calculations

If I want to write a complicated, ugly or messy code

Other disadvantages:

Less spread than Perl is Ruby is relatively slow

January 18, 2010 Radek Mika - Unicorn College 17

Page 18: Programming language Ruby and the Rails framework

…some code examples

January 18, 2010 Radek Mika - Unicorn College 18

And finally…

Page 19: Programming language Ruby and the Rails framework

Clear Syntax# Output "UPPER"puts "upper".upcase

# Output the absolute value of -5:puts -5.abs

# Output "Ruby Rocks!" 5 times5.times do

puts "Ruby Rocks!"endSource: http://pastie.org/785234

January 18, 2010 Radek Mika - Unicorn College 19

Page 20: Programming language Ruby and the Rails framework

Classes and Methods#Classes begin with class and end with end:# The Greeter classclass Greeterend

#Methods begin with def and end with end:# The salute methoddef saluteend

Source: http://pastie.org/785249

January 18, 2010 Radek Mika - Unicorn College 20

Page 21: Programming language Ruby and the Rails framework

Classes and Methods# The Greeter classclass Greeter

def initialize(greeting)@greeting = greeting

end

def salute(name)puts "#{@greeting} #{name}!"

endend

# Initialize our Greeterg = Greeter.new("Hello")

# Output "Hello World!"g.salute("World")

Source: http://pastie.org/785258 - classes

January 18, 2010 Radek Mika - Unicorn College 21

Page 22: Programming language Ruby and the Rails framework

If Statements# if with several branchesif account.total > 100000puts "large account"

elsif account.total > 25000puts "medium account"

elseputs "small account„

endSources: http://pastie.org/785268

January 18, 2010 Radek Mika - Unicorn College 22

Page 23: Programming language Ruby and the Rails framework

Case Statements# A simple case/when statementcase namewhen "John"puts "Howdy John!"

when "Ryan"puts "Whatz up Ryan!"

elseputs "Hi #{name}!"

endSources: http://pastie.org/785278

January 18, 2010 Radek Mika - Unicorn College 23

Page 24: Programming language Ruby and the Rails framework

Regular Expressions#Ruby supports Perl-style regular expressions:# Extract the parts of a phone number

phone = "123-456-7890"

if phone =~ /(\d{3})-(\d{3})-(\d{4})/ext = $1city = $2num = $3

end

Sources: http://pastie.org/785732

January 18, 2010 Radek Mika - Unicorn College 24

Page 25: Programming language Ruby and the Rails framework

Regular Expressions# Case statement with regular expressioncase langwhen /ruby/i

puts "Matz created Ruby!"when /perl/i

puts "Larry created Perl!"else

puts "I don't know who created #{lang}."end

Sources: http://pastie.org/785738

January 18, 2010 Radek Mika - Unicorn College 25

Page 26: Programming language Ruby and the Rails framework

Ruby Blocks# Print out a list of people from# each person in the Arraypeople.each do |person|

puts "* #{person.name}"end

# A block using the bracket syntax 5.times { puts "Ruby rocks!" }

# Custom sorting[2,1,3].sort! { |a, b| b <=> a }

Sources: http://pastie.org/pastes/785239

January 18, 2010 Radek Mika - Unicorn College 26

Presenter
Presentation Notes
Blocks: Ruby’s Secret Sauce Blocks are like anonymous methods:
Page 27: Programming language Ruby and the Rails framework

Yield to the Block!# define the thrice methoddef thrice

yieldyieldyield

end

# Output "Blocks are cool!" three timesthrice { puts "Blocks are cool!" }

#This example use yield from within a method to #hand control over to a block:Sources: http://pastie.org/785774

January 18, 2010 Radek Mika - Unicorn College 27

Presenter
Presentation Notes
Blocks: Ruby’s Secret Sauce Blocks are like anonymous methods:
Page 28: Programming language Ruby and the Rails framework

Blocks with Parameters# redefine the thrice methoddef thrice

yield(1)yield(2)yield(3)

end

# Output "Blocks are cool!" three times,# prefix it with the countthrice { | i |

puts "#{i}: Blocks are cool!"}Sources: http://pastie.org/785789

January 18, 2010 Radek Mika - Unicorn College 28

Presenter
Presentation Notes
Blocks: Ruby’s Secret Sauce Blocks are like anonymous methods:
Page 29: Programming language Ruby and the Rails framework

What about Ruby on Rails?

January 18, 2010 Radek Mika - Unicorn College 29

Enough talking about Ruby!...

Page 30: Programming language Ruby and the Rails framework

Ruby on Rails Web framework

An extremely productive web-applicationframework that is written in Ruby byDavid Hansson

Includes everything needed to create database-driven web applications according to the Model-View-Control pattern of separation

So-called reason of spreading ruby

30Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 31: Programming language Ruby and the Rails framework

Ruby on Rails

MVC

Convention over Configurations

Don’t Repeat Yourself (DRY)

31Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 32: Programming language Ruby and the Rails framework

History

Predominantly written by David H. Hannson Talented designer His dream is to change the world A 37signals.com principal – World class designers

Since 2005

32Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 33: Programming language Ruby and the Rails framework

Model – View - Controller MVC is an architectural pattern, used not only for building web

applications

Model classes are the "smart" domain objects (such as Account, Product, Person, Post) that hold business logic and know how to persist themselves to a database

Views are HTML templates

Controllers handle incoming requests (such as Save New Account, Update Product, Show Post) by manipulating the model and directing data to the view

33Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 34: Programming language Ruby and the Rails framework

Active RecordObject/Relational Mapping Framework = Active Record

Automatic mapping between columns and class attributes

Declarative configuration via macros Dynamic finders Associations, Aggregations, Tree and List Behaviors Locking Lifecycle Callbacks Single-table inheritance supported Validation rules

34Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 35: Programming language Ruby and the Rails framework

From Controller to ViewRails gives you many rendering options

Default template rendering Just follow naming conventions and magic happens.

Explicitly render to particular action

Redirect to another action

Render a string response (or no response)

35Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 36: Programming language Ruby and the Rails framework

View TemplateERB –Embedded Ruby

Similar to JSPs <% and <%= syntax

Easy to learn and teach for designers

Execute in scope of controller

Denoted with .rhtml extension

36Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 37: Programming language Ruby and the Rails framework

View Template

XmlMarkup –Programmatic View Construction

Great for writing xhtml and xml content

Denoted with .rxml extension

Embeddable in ERB templates

37Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 38: Programming language Ruby and the Rails framework

And Much More… Templates and partials Pagination Caching (page, fragment, action) Helpers Routing with routes.rb Exceptions Unit testing ActiveSupport API (date conversion, time calculations) ActionMailer API ActionWebService API Rake

38Radek Mika - Unicorn CollegeJanuary 18, 2010

Page 39: Programming language Ruby and the Rails framework

Sources

39Radek Mika - Unicorn CollegeJanuary 18, 2010

Ruby on Rails (Agile Atlanta Group) – Obie Fernandez – May 10 ’05 Ruby Language Overview – Muhamad Admin Rastgee Ruby on Rails – Curt Hibbs Workin’ on the Rails Road – Obie Fernandez Get to the Point! (Development with Ruby on Rails) – Ryan Platte, John W. Long

Ruby speed comparison (http://is.gd/70hjD)

http://en.wikipedia.org/wiki/Ruby_on_Rails

Page 40: Programming language Ruby and the Rails framework

External Links

40Radek Mika - Unicorn CollegeJanuary 18, 2010

http://ruby-lang.org – official website

http://www.ruby-doc.org/ - Ruby doc project

http://rubyforge.org/ - projects in Ruby

http://www.rubycentral.com/book/ - online book Programming Ruby

Full Ruby on Rails Tutorial

Euruko 2008 - videos from European Ruby Conference 2008 in Prague on avc-cvut.cz (Czech)

Page 41: Programming language Ruby and the Rails framework

Between Q&A…

41Radek Mika - Unicorn CollegeJanuary 18, 2010

… do you still think that you have a fast computer? :)

… you can run this code …

Page 42: Programming language Ruby and the Rails framework

Acknowledgments & Contact

42Radek Mika - Unicorn CollegeJanuary 18, 2010

Special thanks to Mgr. Veronika Kaplanová for English correction.

Radek [email protected]

@radekmika (twitter)

Page 43: Programming language Ruby and the Rails framework

43Radek Mika - Unicorn CollegeJanuary 18, 2010