Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
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  ...
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</require...
Annotations
import java.lang.reflect.*;

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

    public Book (Stri...
import   java.lang.annotation.ElementType;
import   java.lang.annotation.Retention;
import   java.lang.annotation.Retentio...
module Validations
  def validates_presence_of(*attributes)
    # ...
  end

  def validates_size_of(*attributes)
    # .....
Nella mia esperienza,
 la cosa più affascinante è che i
programmi Ruby ben scritti sono
già un DSL, a causa della sintassi...
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 :carro...
Top Level
namespace :db do
  desc quot;Create a database connection.quot;
  task :connect do
    # ...
  end

  desc quot;...
Sandbox
ActiveRecord::Schema.define do
  create_table :bunnies do |t|
    t.column :name, :string, :null => false
  end
en...
Piattaforma Java
Java => JRuby
require 'java'
include_class 'java.util.Random'

class JavaRandom
  def self.next
    @@r ||= Random.new
   ...
JRuby => Java
import org.jruby.*;

public class UsingRuby {
  public static void main(String[] args) {
    Ruby runtime = ...
www.lucaguidi.com
www.sourcesense.com
  www.pronetics.it
Nächste SlideShare
Wird geladen in …5
×

JRuby e DSL

2.483 Aufrufe

Veröffentlicht am

Panoramica sui DSL e sul loro utilizzo con i linguaggi dinamici.

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

JRuby e DSL

  1. 1. JRuby e DSLs Luca Guidi - Sourcesense Java Day Roma - 1 Dic 2007
  2. 2. DSLs cosa sono?
  3. 3. Domain Specific Language
  4. 4. DSLs • single purpose • singolo dominio
  5. 5. Linguaggi di programmazione • general purpose • multi dominio
  6. 6. YACC (1975) Stephen C. Johnson
  7. 7. UNIX scripting
  8. 8. ls | grep dsl >> ~/list.txt
  9. 9. favoriscono le ontologie
  10. 10. Ontologia: rappresentazione strutturata delle informazioni espressa tramite gerarchie e relazioni tra classi.
  11. 11. Behavior Driven Development
  12. 12. 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
  13. 13. should
  14. 14. Secondo Martin Fowler i DSL sono: • Esterni (XML) • Interni (Smalltalk, Ruby)
  15. 15. Java • Non dinamico • Strongly typed
  16. 16. <?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>
  17. 17. Annotations
  18. 18. 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() ) ); } }
  19. 19. 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; }
  20. 20. 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
  21. 21. 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
  22. 22. Ruby • Symbol • Block • Module • Splat • eval, instance_eval, class_eval • define_method, alias_method
  23. 23. Ruby DSLs
  24. 24. Method Class class Carrot < ActiveRecord::Base belongs_to :bunny end class Bunny < ActiveRecord::Base has_many :carrots end
  25. 25. 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
  26. 26. 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
  27. 27. Piattaforma Java
  28. 28. Java => JRuby require 'java' include_class 'java.util.Random' class JavaRandom def self.next @@r ||= Random.new @@r.nextInt end end JavaRandom.next # => 23
  29. 29. JRuby => Java import org.jruby.*; public class UsingRuby { public static void main(String[] args) { Ruby runtime = Ruby.getDefaultInstance(); runtime.evalScript(quot;puts 1 + 2quot;); } }
  30. 30. www.lucaguidi.com www.sourcesense.com www.pronetics.it

×