SlideShare a Scribd company logo
Ruby
Play
2.0
       X    On
           Rails

               by @danicuki
Documentation
http://www.playframework.com/documentation/   http://guides.rubyonrails.org/
Installing

$   apt-get install java         $	
  apt-­‐get	
  install	
  ruby
$   wget play-2.1.0.zip          $	
  apt-­‐get	
  install	
  rubygems
$   unzip play-2.1.0.zip         $	
  gem	
  install	
  rails
$   export
    PATH=$PATH:/pathToPlay



http://www.playframework.com/
documentation/2.1.0/Installing
First App


$ play new myapp     $	
  rails	
  new	
  myapp
Project Structure
app/                      app/
 ! assets                  ! assets
     ! stylesheets             ! stylesheets
     ! javascripts             ! javascripts
 ! controllers             ! controllers
 ! models                  ! helpers
 ! views                   ! mailers
conf/                      ! models
 ! application.conf        ! views
 ! routes                 config/
public/                    ! application.rb
project                    ! routes.rb
 ! build.properties        ! database.yml
 ! Build.scala            config.ru
 ! plugins.sbt            db
lib/                      GemFile
logs/                     lib/
target/                   log/
                          public/
 ! scala-2.10.0           script/
test/                     test/
                          tmp/
                          vendor/
Console
Dependencies
Maven
project/Build.scala
val appDependencies = Seq(
  "postgresql" % "postgresql" % "8.4-702.jdbc4"
  "org.mockito" % "mockito-all" % "1.8.5" % "test"
)

RubyGems
Gemfile
gem "mysql2"
group :test do
  gem "webmock"
end
Dependencies Support
       429.000 jars
     mvnrepository.com




       52,468 gems
      rubygems.org
Views Templates
STE    (Scala Template Engine)

@(title: String)(content: Html)
<title>
  @Messages("site.title") - @title
</title>
@{(video  "title")(0).text}


ERB      (Embedded Ruby)

<title>
	
  	
  <%=	
  t("site.title")	
  %>	
  -­‐	
  <%=	
  yield(:title)	
  %>
</title>
<%=	
  video['title']	
  %>
js / css
<link rel="stylesheet" href="@routes.Assets.at("stylesheets/grid.css")">
<link rel="stylesheet" href="@routes.Assets.at("stylesheets/style.css")">
<link rel="stylesheet" href="@routes.Assets.at("stylesheets/galleriffic.css")">
<script src="@routes.Assets.at("javascripts/applicaton.js")" type="text/javascript" />
<script src="@routes.Assets.at("javascripts/jquery.js")" type="text/javascript" />




 <%=	
  javascript_include_tag	
  :all,	
  :cache	
  =>	
  true	
  %>	
  
 <%=	
  stylesheet_link_tag	
  :all,	
  :cache	
  =>	
  true	
  %>
Links
<li class="menu">
  <a href="@routes.Application.cantora">@Messages("site.singer")</a>
</li>




 <li	
  class="menu">
 	
  	
  <%=	
  link_to	
  "#{t('site.singer')}",	
  {:controller	
  =>	
  "cantora"}	
  %>
 </li>
Routes
GET     /cantora         controllers.Application.cantora
GET     /musicas         controllers.Application.music
GET     /agenda          controllers.Application.shows
GET     /novidades       controllers.Application.news

GET   /clients/:id         controllers.Clients.show(id: Long)




map.connect	
  ':controller/:action/:id'
Controllers
def   cantora = Action { implicit request =>; Ok(views.html.cantora()) }
def   music = Action { implicit request =>; Ok(views.html.music()) }
def   shows = Action { implicit request => Ok(views.html.shows()) }
def   news = Action { implicit request => Ok(views.html.news()) }

/app/views/[method_name].scala.html



class	
  CantoraController	
  <	
  ApplicationController
        def	
  index
	
  	
  end
end
class	
  NewsController	
  <	
  ApplicationController
	
   def	
  index
	
   end
end
/app/views/[controller_name]/index.erb.html
Databases
conf/application.conf:

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/playdb"
db.default.user=playdbuser
db.default.pass="a strong password"




config/database.yml:

development:
	
  	
  adapter:	
  mysql2
	
  	
  database:	
  blog_development
	
  	
  username:	
  root
	
  	
  password:
SQL API:                          Databases
val countries = SQL("Select name,population from Country")().collect {
  case Row("France", _) => France()
  case Row(name:String, pop:Int) if(pop > 1000000) => BigCountry(name)
  case Row(name:String, _) => SmallCountry(name)
}

val result: Int =
  SQL("delete from City where id = {id}").on(“id” -> 99).executeUpdate()

Active	
  Record:

Country.all.map	
  do	
  |c|
	
  	
  if	
  (c.name	
  ==	
  “France”)
	
  	
  	
  	
  France.new
	
  	
  else	
  if	
  (c.pop	
  >	
  1000000)    Country.delete(99)
	
  	
  	
  	
  BigCountry.new(c.name)
	
  	
  else
	
  	
  	
  	
  SmallCountry.new(c.name)
	
  	
  end
end
Databases
                                                OGH
                              (Old Good Hibernate)


Active	
  Record:

Country.all.map	
  do	
  |c|
	
  	
  if	
  (c.name	
  ==	
  “France”)
	
  	
  	
  	
  France.new
	
  	
  else	
  if	
  (c.pop	
  >	
  1000000)         Country.delete(99)
	
  	
  	
  	
  BigCountry.new(c.name)
	
  	
  else
	
  	
  	
  	
  SmallCountry.new(c.name)
	
  	
  end
end
Migrations / Evolutions
# --- !Ups
CREATE TABLE Users (
    id bigint(20) NOT NULL AUTO_INCREMENT,
    email varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    PRIMARY KEY (id)
);

# --- !Downs                                                               run: AUTOMATIC!
DROP TABLE Users;                                  conf/evolutions/{database name}/{#}.sql


class	
  CreateUsers	
  <	
  ActiveRecord::Migration
	
  	
  def	
  up
	
  	
  	
  	
  create_table	
  :users	
  do	
  |t|
	
  	
  	
  	
  	
  	
  t.string	
  :email
	
  	
  	
  	
  	
  	
  t.string	
  :password
	
  	
  	
  	
  	
  	
  t.timestamps
	
  	
  	
  	
  end
	
  	
  end
	
  	
  def	
  down
	
  	
  	
  	
  drop_table	
  :users                                  run:	
  rake	
  db:migrate
	
  	
  end                                         db/migrate/{timestamp}_create_users.rb
end
Web Services
def videos = Action { implicit request =>
  Async {
    val uri = "http://gdata.youtube.com/feed.xml"
    WS.url(uri).get().map { response =>
      Ok(views.html.videos(response.xml  "entry"))
    }
  }
}



class	
  VideosController	
  <	
  ApplicationController
	
  	
  def	
  index
	
  	
  	
  	
  uri	
  =	
  "http://gdata.youtube.com/feed.xml"
	
  	
  	
  	
  video_feed	
  =	
  RestClient.get(uri)
	
  	
  	
  	
  @videos	
  =	
  Hash.from_xml(video_feed)['feed']['entry']
	
  	
  end
end
XML
Ok(views.html.videos(response.xml  "entry"))

@(videos: scala.xml.NodeSeq)

@videos.map { video =>
    <a href="@{ ((video  "group")  "player")(0).attribute("url")}">
       @{(video  "title")(0).text}
    </a>
}


@videos	
  =	
  Hash.from_xml(video_feed)['feed']['entry']

<%	
  @videos.each	
  do	
  |video|	
  %>
	
  	
  	
  	
  	
  <a	
  href="<%=	
  video['group']['player']['url']	
  %>">
	
  	
  	
  	
  	
  	
  	
  	
  	
  <%=	
  video['title']	
  %>
	
  	
  	
  	
  	
  </a>
<%	
  end	
  %>
WS.url("https://graph.facebook.com/daniella.alcarpe/albums").get().map	
  {	
  response	
  =>
	
  	
  	
  	
  val	
  albuns	
  =	
  (response.json	
  	
  "data").
	
  	
  	
  	
  	
  	
  as[List[JsObject]].filter(album	
  =>
	
  	
  	
  	
  	
  	
  	
  	
  (album	
  	
  "description").toString.equals(""*""))
	
  
	
  	
  	
  	
  val	
  photos	
  =	
  albuns.map	
  {	
  album	
  =>
	
  	
  	
  	
  	
  	
  WS.url("https://graph.facebook.com/"	
  +	
  (album	
  	
  "id").toString.replace(""",	
  "")	
  +	
  
"/photos").get().map	
  {	
  response2	
  =>
	
  	
  	
  	
  	
  	
  	
  	
  (response2.json	
  	
  "data").as[List[JsObject]].map	
  {	
  photo	
  =>
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ((photo	
  	
  "images")(3)	
  	
  "source").toString.replace(""",	
  "")
	
  	
  	
  	
  	
  	
  	
  	
  }
	
  	
  	
  	
  	
  	
  }
	
  	
  	
  	
  }
	
  	
  Ok(views.html.photos(photos))


                                                                                                         JSON
}


@albuns_photos	
  =	
  {}
	
  	
  	
  	
  @albuns	
  =	
  []
	
  	
  	
  	
  albuns	
  =	
  JSON.parse(RestClient.get("https://graph.facebook.com/
daniella.alcarpe/albums"))["data"]
	
  	
  	
  	
  albuns.each	
  do	
  |album|
	
  	
  	
  	
  	
  	
  if	
  (album['description']	
  ==	
  "*")
	
  	
  	
  	
  	
  	
  	
  	
  photos	
  =	
  JSON.parse(RestClient.get("https://graph.facebook.com/
#{album['id']}/photos"))["data"]
	
  	
  	
  	
  	
  	
  	
  	
  albuns_photos	
  =	
  photos.map	
  {|p|	
  p["images"][3]["source"]}
	
  	
  	
  	
  	
  	
  	
  	
  album['photos']	
  =	
  albuns_photos
	
  	
  	
  	
  	
  	
  	
  	
  @albuns	
  <<	
  album
	
  	
  	
  	
  	
  	
  end
	
  	
  	
  	
  end
Cache
def videos = Cached("videos", 18000) {
  Action {
  ...
  }
}




class	
  VideosController	
  <	
  ApplicationController
	
  	
  	
  	
  caches_action	
  :index,	
  :expires_in	
  =>	
  1.day
end
Unit Tests
@Test def myTest() {
  val array = List(1, 2, 3)
  assert(array(0) === 1)
}




test	
  "my	
  test"	
  do
	
  	
  array	
  =	
  [1,	
  2,	
  3]
	
  	
  assert_equal	
  1,	
  array.first
end
Specs
class HelloWorldSpec extends Specification {
  "The 'Hello world' string" should {
    "contain 11 characters" in {
                                                                   specs2
      "Hello world" must have size(11)
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
                                                                   autotest:
  }
    }
                                                                   $ ~ test
}



describe	
  "HelloWorldSpec"	
  do
  context	
  "The	
  'Hello	
  world'	
  string	
  should"	
  do
  	
  	
  it	
  "contain 11 characters"	
  do                      rspec
  	
  	
  	
  	
  "Hello	
  world".size.should	
  eq(11)
  	
  	
  end
  	
  	
  it	
  "end with 'world'"	
  do
  	
  	
  	
  	
  "Hello	
  world".should	
  end_with("world")     $ autospec
  	
  	
  end
  end
end
In Procfile:
                                 Deploy
web: target/start -Dhttp.port=${PORT} -DapplyEvolutions.default=true -
Ddb.default.url=${DATABASE_URL} -Ddb.default.driver=org.postgresql.Driver

In project/Build.scala:
val appDependencies = Seq(
  "postgresql" % "postgresql" % "8.4-702.jdbc4"
)

$ heroku create
$ git push heroku master




$	
  heroku	
  create
$	
  git	
  push	
  heroku	
  master
Adendo




Scala    X       Ruby


                    by @danicuki
Lambda

list.filter(_ < 100)      list.select	
  do	
  |el|
                          	
  	
  el	
  <	
  100
list.filter {             end	
  
  el: Int => (el < 100)
}
Types

Static:                  Dynamic:

val a = new              a	
  =	
  Hash.new
  HashMap[Int, String]   a	
  =	
  “BOO!”
Pattern Matching
                             require	
  'case'
                             def	
  matchTest	
  x
                             	
  case	
  x
                             	
  when	
  7
def myMatch(x: Any): Any =   	
  	
  	
  "seven"
x match {                    	
  when	
  "string"
  case 7 => “seven”
                             	
  	
  	
  0
  case “string” => 0
                             	
  when	
  Case::All[Integer]
  case y:Int => “no.”
  case 2 :: tail => tail     	
  	
  	
  "no."
}                            	
  when	
  
                             Case::Array[2,Case::Any]
                             	
  	
  	
  x[1..-­‐1]
                             	
  end
                             end
Monkey Patch
class MySuperString(original: String) {
  def myMethod = "yess!!!"
}
implicit def string2super(x: String) =
  new MySuperString(x)

println("a".myMethod) // => yess!!!


class	
  String
	
  	
  def	
  my_method
	
  	
  	
  "yess!!!"
	
  	
  end
end

puts	
  "a".my_method	
  #	
  =>	
  yess!!!
Dynamic Calls
 class Animal extends Dynamic                {
   def _select_(name: String)                = println("Animal says " + name)
   def _invoke_(name: String,                args: Any*) = {
     println("Animal wants to                " + name + args.mkString(", "))
     this
   }            val animal =        new Animal
 }              animal.qualk        // => Animal says qualk
                       animal.say("hello") // => Animal wants to say hello
class	
  Animal
	
  	
  def	
  method_missing	
  name,	
  *args
	
  	
  	
  	
  if	
  args.empty?
	
  	
  	
  	
  	
  	
  puts	
  "Animal	
  says	
  "	
  +	
  name.to_s
	
  	
  	
  	
  else
	
  	
  	
  	
  	
  	
  puts	
  "Animal	
  wants	
  to	
  "	
  +	
  name.to_s	
  +	
  	
  args.join(",	
  ")
	
  	
  	
  	
  end
	
  	
  	
  	
  self               animal	
  =	
  Animal.new
	
  	
  end                        animal.qualk	
  #	
  =>	
  Animal	
  says	
  :	
  qualks	
  !
end                                animal.say("hello")	
  #	
  =>	
  Animal	
  wants	
  to	
  say	
  hello
Modules / Traits
trait PimpMyClass {
  def myMethod = println("myMethod")
}
class IncludeTrait extends PimpMyClass
(new IncludeTrait).myMethod


module	
  PimpMyClass
	
  	
  def	
  my_method
	
  	
  	
  	
  puts	
  "my_method"
	
  	
  end
end
class	
  IncludeModule
	
  	
  include	
  PimpMyClass
end
IncludeModule.new.my_method
Duck Typing
class     Duck {                 def ActAsADuck(a: { def quack; def walk })= {
  def     quack = ...                 a.quack
  def     walk = ...                  a.walk
                                 }
}
class     Platypus {             val duck = new Duck
  def     quack = ...            val platypus = new Platypus
  def     walk = ...             ActAsADuck(duck)
}                                ActAsADuck(platypus)

class	
  Duck                    def	
  act_as_a_duck	
  animal
	
  	
  def	
  quack;	
  end     	
  	
  animal.quack
	
  	
  def	
  walk;	
  end      	
  	
  animal.walk
end                              end

class	
  Platypus                duck	
  =	
  Duck.new
	
  	
  def	
  quack;	
  end     platypus	
  =	
  Platypus.new
	
  	
  def	
  walk;	
  end      act_as_a_duck(duck)
end                              act_as_a_duck(platypus)
Actors
Performance
                                80
               70.74
                                                     Scala
                            60                       JRuby
                                                     Ruby


       39.74               40



                       20


2.07
                       0

                                     Source : http://shootout.alioth.debian.org
References
✦ http://www.playframework.com/documentation
✦ http://guides.rubyonrails.org/
✦ http://www.agileandart.com/
✦ http://www.slideshare.net/El_Picador/scala-vs-ruby
Obrigado!
www.agileandart.com
    @danicuki

More Related Content

PDF
Node.js vs Play Framework (with Japanese subtitles)
PPTX
JavaFX 2 and Scala - Like Milk and Cookies (33rd Degrees)
PDF
The JavaFX Ecosystem
PDF
The DOM is a Mess @ Yahoo
PDF
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
KEY
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
PDF
HTML5 JavaScript APIs
PDF
Introduction to rest.li
Node.js vs Play Framework (with Japanese subtitles)
JavaFX 2 and Scala - Like Milk and Cookies (33rd Degrees)
The JavaFX Ecosystem
The DOM is a Mess @ Yahoo
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
HTML5 JavaScript APIs
Introduction to rest.li

What's hot (20)

PDF
Mobile Open Day: React Native: Crossplatform fast dive
PPTX
Java Play Restful JPA
PPTX
Java Play RESTful ebean
PDF
Rails Security
PPT
PDF
Introduction to Flask Micro Framework
PDF
Keeping the frontend under control with Symfony and Webpack
PPTX
Javascript Testing with Jasmine 101
KEY
Building a real life application in node js
PPT
Presentation
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
PPT
Migrating PriceChirp to Rails 3.0: The Pain Points
PDF
JavaOne - The JavaFX Community and Ecosystem
PDF
An Introduction to Celery
PDF
Debugging on rails
PPT
Intro to-ant
PDF
Django Rest Framework and React and Redux, Oh My!
PDF
Django Celery - A distributed task queue
ODP
Europython 2011 - Playing tasks with Django & Celery
PPTX
Good karma: UX Patterns and Unit Testing in Angular with Karma
Mobile Open Day: React Native: Crossplatform fast dive
Java Play Restful JPA
Java Play RESTful ebean
Rails Security
Introduction to Flask Micro Framework
Keeping the frontend under control with Symfony and Webpack
Javascript Testing with Jasmine 101
Building a real life application in node js
Presentation
We Are All Testers Now: The Testing Pyramid and Front-End Development
Migrating PriceChirp to Rails 3.0: The Pain Points
JavaOne - The JavaFX Community and Ecosystem
An Introduction to Celery
Debugging on rails
Intro to-ant
Django Rest Framework and React and Redux, Oh My!
Django Celery - A distributed task queue
Europython 2011 - Playing tasks with Django & Celery
Good karma: UX Patterns and Unit Testing in Angular with Karma
Ad

Similar to Play vs Rails (20)

PDF
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
PDF
Connecting the Worlds of Java and Ruby with JRuby
PPT
Play!ng with scala
PDF
Ruby on Rails : 簡介與入門
PDF
PDF
Ruby on Rails - Introduction
PDF
G*ワークショップ in 仙台 Grails(とことん)入門
PDF
td_mxc_rubyrails_shin
PDF
td_mxc_rubyrails_shin
PDF
When To Use Ruby On Rails
KEY
Desarrollando aplicaciones web en minutos
KEY
Intro to Ruby on Rails
PDF
Web application intro + a bit of ruby (revised)
PDF
Rails workshop for Java people (September 2015)
PDF
Groovy - Grails as a modern scripting language for Web applications
PDF
Content-Driven Web Applications with Magnolia CMS and Ruby on Rails
KEY
Why ruby and rails
PDF
Play framework
PDF
JRuby, Ruby, Rails and You on the Cloud
PDF
Ruby on rails探索
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Connecting the Worlds of Java and Ruby with JRuby
Play!ng with scala
Ruby on Rails : 簡介與入門
Ruby on Rails - Introduction
G*ワークショップ in 仙台 Grails(とことん)入門
td_mxc_rubyrails_shin
td_mxc_rubyrails_shin
When To Use Ruby On Rails
Desarrollando aplicaciones web en minutos
Intro to Ruby on Rails
Web application intro + a bit of ruby (revised)
Rails workshop for Java people (September 2015)
Groovy - Grails as a modern scripting language for Web applications
Content-Driven Web Applications with Magnolia CMS and Ruby on Rails
Why ruby and rails
Play framework
JRuby, Ruby, Rails and You on the Cloud
Ruby on rails探索
Ad

More from Daniel Cukier (20)

PDF
Solidity: Zero to Hero Corporate Training
PDF
Spring e Injeção de Dependência
PDF
Pair programming
PDF
Eficiency and Low Cost: Pro Tips for you to save 50% of your money with Googl...
PDF
Startup Communities: From Nascence to Maturity
PDF
Technology Startups Ecosystem in China - Lessons to other ecosystems
PDF
Software Startup Ecosystems Evolution - The New York City Case Study
PDF
Maturity model for Startup Ecosystems
PDF
Why Google Cloud is so special? Stories from a cloud user
PDF
Software Architectures for a Single Person Team
PDF
Startup Communities
PDF
Introduction to Functional Programming with Scala
PDF
O dia a dia de uma Startup
KEY
Injeção de Dependência e Testes com Dublês
PDF
Selecting Empirical Methods for Software Engineering
PDF
Is Computer Science Science?
PDF
Ruby Robots
PDF
Better Science Through Art
PDF
Designed as Designer
PDF
When Should You Consider Meta Architectures
Solidity: Zero to Hero Corporate Training
Spring e Injeção de Dependência
Pair programming
Eficiency and Low Cost: Pro Tips for you to save 50% of your money with Googl...
Startup Communities: From Nascence to Maturity
Technology Startups Ecosystem in China - Lessons to other ecosystems
Software Startup Ecosystems Evolution - The New York City Case Study
Maturity model for Startup Ecosystems
Why Google Cloud is so special? Stories from a cloud user
Software Architectures for a Single Person Team
Startup Communities
Introduction to Functional Programming with Scala
O dia a dia de uma Startup
Injeção de Dependência e Testes com Dublês
Selecting Empirical Methods for Software Engineering
Is Computer Science Science?
Ruby Robots
Better Science Through Art
Designed as Designer
When Should You Consider Meta Architectures

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf

Play vs Rails

  • 1. Ruby Play 2.0 X On Rails by @danicuki
  • 2. Documentation http://www.playframework.com/documentation/ http://guides.rubyonrails.org/
  • 3. Installing $ apt-get install java $  apt-­‐get  install  ruby $ wget play-2.1.0.zip $  apt-­‐get  install  rubygems $ unzip play-2.1.0.zip $  gem  install  rails $ export PATH=$PATH:/pathToPlay http://www.playframework.com/ documentation/2.1.0/Installing
  • 4. First App $ play new myapp $  rails  new  myapp
  • 5. Project Structure app/ app/ ! assets ! assets ! stylesheets ! stylesheets ! javascripts ! javascripts ! controllers ! controllers ! models ! helpers ! views ! mailers conf/ ! models ! application.conf ! views ! routes config/ public/ ! application.rb project ! routes.rb ! build.properties ! database.yml ! Build.scala config.ru ! plugins.sbt db lib/ GemFile logs/ lib/ target/ log/ public/ ! scala-2.10.0 script/ test/ test/ tmp/ vendor/
  • 7. Dependencies Maven project/Build.scala val appDependencies = Seq( "postgresql" % "postgresql" % "8.4-702.jdbc4" "org.mockito" % "mockito-all" % "1.8.5" % "test" ) RubyGems Gemfile gem "mysql2" group :test do gem "webmock" end
  • 8. Dependencies Support 429.000 jars mvnrepository.com 52,468 gems rubygems.org
  • 9. Views Templates STE (Scala Template Engine) @(title: String)(content: Html) <title> @Messages("site.title") - @title </title> @{(video "title")(0).text} ERB (Embedded Ruby) <title>    <%=  t("site.title")  %>  -­‐  <%=  yield(:title)  %> </title> <%=  video['title']  %>
  • 10. js / css <link rel="stylesheet" href="@routes.Assets.at("stylesheets/grid.css")"> <link rel="stylesheet" href="@routes.Assets.at("stylesheets/style.css")"> <link rel="stylesheet" href="@routes.Assets.at("stylesheets/galleriffic.css")"> <script src="@routes.Assets.at("javascripts/applicaton.js")" type="text/javascript" /> <script src="@routes.Assets.at("javascripts/jquery.js")" type="text/javascript" /> <%=  javascript_include_tag  :all,  :cache  =>  true  %>   <%=  stylesheet_link_tag  :all,  :cache  =>  true  %>
  • 11. Links <li class="menu"> <a href="@routes.Application.cantora">@Messages("site.singer")</a> </li> <li  class="menu">    <%=  link_to  "#{t('site.singer')}",  {:controller  =>  "cantora"}  %> </li>
  • 12. Routes GET /cantora controllers.Application.cantora GET /musicas controllers.Application.music GET /agenda controllers.Application.shows GET /novidades controllers.Application.news GET /clients/:id controllers.Clients.show(id: Long) map.connect  ':controller/:action/:id'
  • 13. Controllers def cantora = Action { implicit request =>; Ok(views.html.cantora()) } def music = Action { implicit request =>; Ok(views.html.music()) } def shows = Action { implicit request => Ok(views.html.shows()) } def news = Action { implicit request => Ok(views.html.news()) } /app/views/[method_name].scala.html class  CantoraController  <  ApplicationController def  index    end end class  NewsController  <  ApplicationController   def  index   end end /app/views/[controller_name]/index.erb.html
  • 15. SQL API: Databases val countries = SQL("Select name,population from Country")().collect { case Row("France", _) => France() case Row(name:String, pop:Int) if(pop > 1000000) => BigCountry(name) case Row(name:String, _) => SmallCountry(name) } val result: Int = SQL("delete from City where id = {id}").on(“id” -> 99).executeUpdate() Active  Record: Country.all.map  do  |c|    if  (c.name  ==  “France”)        France.new    else  if  (c.pop  >  1000000) Country.delete(99)        BigCountry.new(c.name)    else        SmallCountry.new(c.name)    end end
  • 16. Databases OGH (Old Good Hibernate) Active  Record: Country.all.map  do  |c|    if  (c.name  ==  “France”)        France.new    else  if  (c.pop  >  1000000) Country.delete(99)        BigCountry.new(c.name)    else        SmallCountry.new(c.name)    end end
  • 17. Migrations / Evolutions # --- !Ups CREATE TABLE Users ( id bigint(20) NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL, password varchar(255) NOT NULL, PRIMARY KEY (id) ); # --- !Downs run: AUTOMATIC! DROP TABLE Users; conf/evolutions/{database name}/{#}.sql class  CreateUsers  <  ActiveRecord::Migration    def  up        create_table  :users  do  |t|            t.string  :email            t.string  :password            t.timestamps        end    end    def  down        drop_table  :users run:  rake  db:migrate    end db/migrate/{timestamp}_create_users.rb end
  • 18. Web Services def videos = Action { implicit request => Async { val uri = "http://gdata.youtube.com/feed.xml" WS.url(uri).get().map { response => Ok(views.html.videos(response.xml "entry")) } } } class  VideosController  <  ApplicationController    def  index        uri  =  "http://gdata.youtube.com/feed.xml"        video_feed  =  RestClient.get(uri)        @videos  =  Hash.from_xml(video_feed)['feed']['entry']    end end
  • 19. XML Ok(views.html.videos(response.xml "entry")) @(videos: scala.xml.NodeSeq) @videos.map { video =>     <a href="@{ ((video "group") "player")(0).attribute("url")}">        @{(video "title")(0).text}     </a> } @videos  =  Hash.from_xml(video_feed)['feed']['entry'] <%  @videos.each  do  |video|  %>          <a  href="<%=  video['group']['player']['url']  %>">                  <%=  video['title']  %>          </a> <%  end  %>
  • 20. WS.url("https://graph.facebook.com/daniella.alcarpe/albums").get().map  {  response  =>        val  albuns  =  (response.json    "data").            as[List[JsObject]].filter(album  =>                (album    "description").toString.equals(""*""))          val  photos  =  albuns.map  {  album  =>            WS.url("https://graph.facebook.com/"  +  (album    "id").toString.replace(""",  "")  +   "/photos").get().map  {  response2  =>                (response2.json    "data").as[List[JsObject]].map  {  photo  =>                    ((photo    "images")(3)    "source").toString.replace(""",  "")                }            }        }    Ok(views.html.photos(photos)) JSON } @albuns_photos  =  {}        @albuns  =  []        albuns  =  JSON.parse(RestClient.get("https://graph.facebook.com/ daniella.alcarpe/albums"))["data"]        albuns.each  do  |album|            if  (album['description']  ==  "*")                photos  =  JSON.parse(RestClient.get("https://graph.facebook.com/ #{album['id']}/photos"))["data"]                albuns_photos  =  photos.map  {|p|  p["images"][3]["source"]}                album['photos']  =  albuns_photos                @albuns  <<  album            end        end
  • 21. Cache def videos = Cached("videos", 18000) {   Action { ...   } } class  VideosController  <  ApplicationController        caches_action  :index,  :expires_in  =>  1.day end
  • 22. Unit Tests @Test def myTest() { val array = List(1, 2, 3) assert(array(0) === 1) } test  "my  test"  do    array  =  [1,  2,  3]    assert_equal  1,  array.first end
  • 23. Specs class HelloWorldSpec extends Specification { "The 'Hello world' string" should { "contain 11 characters" in { specs2 "Hello world" must have size(11) } "end with 'world'" in { "Hello world" must endWith("world") autotest: } } $ ~ test } describe  "HelloWorldSpec"  do context  "The  'Hello  world'  string  should"  do    it  "contain 11 characters"  do rspec        "Hello  world".size.should  eq(11)    end    it  "end with 'world'"  do        "Hello  world".should  end_with("world") $ autospec    end end end
  • 24. In Procfile: Deploy web: target/start -Dhttp.port=${PORT} -DapplyEvolutions.default=true - Ddb.default.url=${DATABASE_URL} -Ddb.default.driver=org.postgresql.Driver In project/Build.scala: val appDependencies = Seq( "postgresql" % "postgresql" % "8.4-702.jdbc4" ) $ heroku create $ git push heroku master $  heroku  create $  git  push  heroku  master
  • 25. Adendo Scala X Ruby by @danicuki
  • 26. Lambda list.filter(_ < 100) list.select  do  |el|    el  <  100 list.filter { end   el: Int => (el < 100) }
  • 27. Types Static: Dynamic: val a = new a  =  Hash.new HashMap[Int, String] a  =  “BOO!”
  • 28. Pattern Matching require  'case' def  matchTest  x  case  x  when  7 def myMatch(x: Any): Any =      "seven" x match {  when  "string" case 7 => “seven”      0 case “string” => 0  when  Case::All[Integer] case y:Int => “no.” case 2 :: tail => tail      "no." }  when   Case::Array[2,Case::Any]      x[1..-­‐1]  end end
  • 29. Monkey Patch class MySuperString(original: String) { def myMethod = "yess!!!" } implicit def string2super(x: String) = new MySuperString(x) println("a".myMethod) // => yess!!! class  String    def  my_method      "yess!!!"    end end puts  "a".my_method  #  =>  yess!!!
  • 30. Dynamic Calls class Animal extends Dynamic { def _select_(name: String) = println("Animal says " + name) def _invoke_(name: String, args: Any*) = { println("Animal wants to " + name + args.mkString(", ")) this } val animal = new Animal } animal.qualk // => Animal says qualk animal.say("hello") // => Animal wants to say hello class  Animal    def  method_missing  name,  *args        if  args.empty?            puts  "Animal  says  "  +  name.to_s        else            puts  "Animal  wants  to  "  +  name.to_s  +    args.join(",  ")        end        self animal  =  Animal.new    end animal.qualk  #  =>  Animal  says  :  qualks  ! end animal.say("hello")  #  =>  Animal  wants  to  say  hello
  • 31. Modules / Traits trait PimpMyClass { def myMethod = println("myMethod") } class IncludeTrait extends PimpMyClass (new IncludeTrait).myMethod module  PimpMyClass    def  my_method        puts  "my_method"    end end class  IncludeModule    include  PimpMyClass end IncludeModule.new.my_method
  • 32. Duck Typing class Duck { def ActAsADuck(a: { def quack; def walk })= { def quack = ... a.quack def walk = ... a.walk } } class Platypus { val duck = new Duck def quack = ... val platypus = new Platypus def walk = ... ActAsADuck(duck) } ActAsADuck(platypus) class  Duck def  act_as_a_duck  animal    def  quack;  end    animal.quack    def  walk;  end    animal.walk end end class  Platypus duck  =  Duck.new    def  quack;  end platypus  =  Platypus.new    def  walk;  end act_as_a_duck(duck) end act_as_a_duck(platypus)
  • 34. Performance 80 70.74 Scala 60 JRuby Ruby 39.74 40 20 2.07 0 Source : http://shootout.alioth.debian.org
  • 35. References ✦ http://www.playframework.com/documentation ✦ http://guides.rubyonrails.org/ ✦ http://www.agileandart.com/ ✦ http://www.slideshare.net/El_Picador/scala-vs-ruby