SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
JRuby e DSLs
    Luca Guidi - Sourcesense
  Java Day Roma - 1 Dic 2007
DSLs cosa sono?
Domain Specific
  Language
DSLs


• single purpose
• singolo dominio
Linguaggi di
   programmazione

• general purpose
• multi dominio
YACC (1975)
 Stephen C. Johnson
UNIX scripting
ls | grep dsl >> ~/list.txt
favoriscono le ontologie
Ontologia: rappresentazione strutturata
 delle informazioni espressa tramite
    gerarchie e relazioni tra classi.
Behavior Driven
 Development
it quot;tracks implicit observable modelsquot; do
  instance = FooObserver.new
  instance.send(:observed_classes).should     include(Foo)
  instance.send(:observed_classes).should_not include(ObservedModel)
end
should
Secondo Martin Fowler
    i DSL sono:

• Esterni (XML)
• Interni (Smalltalk, Ruby)
Java


• Non dinamico
• Strongly typed
<?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
<book>
  <attribute id=quot;titlequot;>
    <required>true</required>
    <length>
       <minimum>1</minimum>
       <maximum>23</maximum>
    </length>
  </attribute>
</book>
Annotations
import java.lang.reflect.*;

public class Book {
   @Size(min=1, max=23)
   protected String title;

    public Book (String title) {
       setTitle(title);
    }

    public String getTitle() {
       return this.title;
    }

    public void setTitle(String title) {
       this.title = title;
    }

    public boolean isValid()
      throws NoSuchFieldException, SecurityException {
       Size size = (Size) this.getClass().getDeclaredField(quot;titlequot;)
                      .getAnnotation(Size.class);
       return ( title != null && ( title.length() >= size.min()
                  || title.length() <= size.max() ) );
    }
}
import   java.lang.annotation.ElementType;
import   java.lang.annotation.Retention;
import   java.lang.annotation.RetentionPolicy;
import   java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Size {
  public int max() default 0;
  public int min() default 0;
}
module Validations
  def validates_presence_of(*attributes)
    # ...
  end

  def validates_size_of(*attributes)
    # ...
  end
end

class Book
  extend Validations
  validates_presence_of :title
  validates_size_of :title, 1..23
end
Nella mia esperienza,
 la cosa più affascinante è che i
programmi Ruby ben scritti sono
già un DSL, a causa della sintassi
          del linguaggio.
         Jamis Buck - 37 signals
Ruby
• Symbol
• Block
• Module
• Splat
• eval, instance_eval, class_eval
• define_method, alias_method
Ruby DSLs
Method Class
class Carrot < ActiveRecord::Base
  belongs_to :bunny
end

class Bunny < ActiveRecord::Base
  has_many :carrots
end
Top Level
namespace :db do
  desc quot;Create a database connection.quot;
  task :connect do
    # ...
  end

  desc quot;Prepare the database for tests.quot;
  task :prepare => :connect do
    # ...
  end
end
Sandbox
ActiveRecord::Schema.define do
  create_table :bunnies do |t|
    t.column :name, :string, :null => false
  end
end

ActiveRecord::Schema.define do
  drop_table :bunnies
end
Piattaforma Java
Java => JRuby
require 'java'
include_class 'java.util.Random'

class JavaRandom
  def self.next
    @@r ||= Random.new
    @@r.nextInt
  end
end

JavaRandom.next # => 23
JRuby => Java
import org.jruby.*;

public class UsingRuby {
  public static void main(String[] args) {
    Ruby runtime = Ruby.getDefaultInstance();
    runtime.evalScript(quot;puts 1 + 2quot;);
  }
}
www.lucaguidi.com
www.sourcesense.com
  www.pronetics.it

Contenu connexe

Tendances

Puppet DSL gotchas, and understandiing Roles & Profiles pattern
Puppet DSL gotchas, and understandiing Roles & Profiles patternPuppet DSL gotchas, and understandiing Roles & Profiles pattern
Puppet DSL gotchas, and understandiing Roles & Profiles pattern
Alex Simenduev
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
Michael Girouard
 

Tendances (20)

Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Puppet DSL gotchas, and understandiing Roles & Profiles pattern
Puppet DSL gotchas, and understandiing Roles & Profiles patternPuppet DSL gotchas, and understandiing Roles & Profiles pattern
Puppet DSL gotchas, and understandiing Roles & Profiles pattern
 
Php Oop
Php OopPhp Oop
Php Oop
 
Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Php 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find UsefulPhp 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find Useful
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
 
Lecture 5: Client Side Programming 1
Lecture 5: Client Side Programming 1Lecture 5: Client Side Programming 1
Lecture 5: Client Side Programming 1
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UXJavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
 
Building Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScriptBuilding Fast, Modern Web Applications with Node.js and CoffeeScript
Building Fast, Modern Web Applications with Node.js and CoffeeScript
 
CoffeeScript By Example
CoffeeScript By ExampleCoffeeScript By Example
CoffeeScript By Example
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2Lecture 6: Client Side Programming 2
Lecture 6: Client Side Programming 2
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
ORMs in Golang
ORMs in GolangORMs in Golang
ORMs in Golang
 

En vedette

Process of communication
Process of communicationProcess of communication
Process of communication
Sweetp999
 

En vedette (6)

Minegems
MinegemsMinegems
Minegems
 
Click to Globalize
Click to GlobalizeClick to Globalize
Click to Globalize
 
Communication - Process & Definition Power Point Presentation
Communication - Process & Definition Power Point PresentationCommunication - Process & Definition Power Point Presentation
Communication - Process & Definition Power Point Presentation
 
COMMUNICATION PROCESS,TYPES,MODES,BARRIERS
COMMUNICATION PROCESS,TYPES,MODES,BARRIERSCOMMUNICATION PROCESS,TYPES,MODES,BARRIERS
COMMUNICATION PROCESS,TYPES,MODES,BARRIERS
 
Process of communication
Process of communicationProcess of communication
Process of communication
 
COMMUNICATION POWERPOINT
COMMUNICATION POWERPOINTCOMMUNICATION POWERPOINT
COMMUNICATION POWERPOINT
 

Similaire à JRuby e DSL

JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Nick Sieger
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
旻琦 潘
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
Andres Almiray
 
AestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in ScalaAestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in Scala
Dmitry Buzdin
 

Similaire à JRuby e DSL (20)

JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Play framework
Play frameworkPlay framework
Play framework
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
Buildr In Action @devoxx france 2012
Buildr In Action @devoxx france 2012Buildr In Action @devoxx france 2012
Buildr In Action @devoxx france 2012
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
Module Magic
Module MagicModule Magic
Module Magic
 
Java Future S Ritter
Java Future S RitterJava Future S Ritter
Java Future S Ritter
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Quick Intro To JRuby
Quick Intro To JRubyQuick Intro To JRuby
Quick Intro To JRuby
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Java, Ruby & Rails
Java, Ruby & RailsJava, Ruby & Rails
Java, Ruby & Rails
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
 
Building maintainable javascript applications
Building maintainable javascript applicationsBuilding maintainable javascript applications
Building maintainable javascript applications
 
AestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in ScalaAestasIT - Internal DSLs in Scala
AestasIT - Internal DSLs in Scala
 
Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011Nick Sieger JRuby Concurrency EMRubyConf 2011
Nick Sieger JRuby Concurrency EMRubyConf 2011
 

Dernier

Dernier (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 

JRuby e DSL