Top Banner
ThoughtWorks ThoughtWorks NEAL FORD software architect / meme wrangler ThoughtWorks [email protected] 3003 Summit Boulevard, Atlanta, GA 30319 www.nealford.com www.thoughtworks.com blog: memeagora.blogspot.com twitter: neal4d comparing groovy & jruby * * please check all knives, guns, pitchforks, and torches at the door
104

Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Feb 12, 2019

Download

Documents

phungtu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

ThoughtWorksThoughtWorks

NEAL FORD software architect / meme wrangler

ThoughtWorks

[email protected] 3003 Summit Boulevard, Atlanta, GA 30319

www.nealford.com

www.thoughtworks.com

blog: memeagora.blogspot.com

twitter: neal4d

comparing groovy & jruby *

* please check all knives, guns, pitchforks, and torches at the door

Page 2: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

housekeepingask questions at the end (insane amount of material!)

download slides from nealford.com

download samples from github.com/nealford

Page 3: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

eigenclass approaches

syntax

closures

framework approach

meta-programming

agenda:

Page 4: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

geek fu test:

Page 5: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Groovy dynamicizesthe JavaTM platform

JRuby brings Ruby tothe JavaTM platform

Page 6: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

strong

staticdynamic

weak

strong

staticdynamic

weak

Page 7: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 8: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

differences & observations

Page 9: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

syntax

Page 10: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

sigils

Page 11: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

this.name

@name

vs

Page 12: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 13: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 14: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

auto-encapsulation?

Page 15: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 16: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 17: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Perl !!!!!!!!!

Page 18: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Ruby has some Perl-isms...

...with significant differences

Page 19: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

closures

Page 20: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 21: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 22: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 23: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 24: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

passing closures

Page 25: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

auto-blocks

Page 26: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

passing closures

Page 27: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

in Groovy, everything is an object...

in Java™, everything is an object...

...except primitives and nulls

in JRuby, everything is an object

except null

everything except...

Page 28: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

null is null

?. handles protected reference

person?.address?.street

Page 29: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

nil is an instance of NilClass

Page 30: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 31: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

switch/case statement

Page 32: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 33: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 34: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

=== isCase()

Page 35: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

“spread” operator

Page 36: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

“elvis” operator

Page 37: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

side effects of encapsulating

Java™

Page 38: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

things added to Object

Page 39: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

supporting collections

Page 40: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

really? really!

Page 41: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

JRuby adds artifacts to “Rubify” Java™

...but doesn’t add collection stuff to Object

JRuby additions

ArrayList

Fixnum

Page 42: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Groovy is optionally typed

Page 43: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 44: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

search exists on Stack

but not on ArrayListor List!

Page 45: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

interfaces & mixins

Page 46: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

“fake out” interfaces via closures using as

allows you to interact with Java™ interfaces

can wrap proxies around them if needed

Groovy & interfaces

Page 47: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 48: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

be careful when implementing with a map:NullPointerException if you forget a method

Page 49: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

named after ice cream mix-ins

mixin

first appeared in symbolic logic’s flavors system

a way to collect functionality

not a form of specialization

Ruby has no interfaces

Page 50: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 51: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

comparisons

Page 52: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

comparisons

Page 53: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 54: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 55: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

comparisons

Page 56: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

violating handshakes

Page 57: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

mixins in Groovy,interfaces in JRuby

Page 58: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 59: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

interfaces in JRuby?

Page 60: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 61: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

meta-programming

Page 62: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

mostly parity (functionally)

very different implementations

Page 63: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

attr_reader, attr_writer, attr_accessor

much of Ruby’s infrastructure is meta-programming

meta-programming methods:

private, protected, public

include

executable declarations

Page 64: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

AST transformations

Page 65: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

implementation

Page 66: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 67: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

optional inclusion

Page 68: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

conditional method

Page 69: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

attributes

Page 70: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

hook methods

Page 71: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

sticky attributes

private, protected, public

Page 72: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

eigenclass

Page 73: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

adding methods via proxies

Page 74: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 75: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Groovy’s eigenclass

Page 76: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

framework approach

Page 77: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

encapsulate & extend

Page 78: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

use Groovy to simplify and humanize them

leverage the existing industrial strength Java™ stack

encapsulate best-of-breed frameworks

encapsulate, then build up

Groovy & Grails

Page 79: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 80: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

abstractionlayersviameta || dsl

Page 81: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

Ruby on Rails = software factories + dsl’s

tends to build things as abstraction layers atop Ruby using meta-programming & dsl techniques

lightweight abstraction layers

Ruby language is always 1 abstraction layer down

Ruby

Page 82: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

case study: builders

Page 83: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

GroovyObjectSupport

BuilderSupport

MarkupBuilder

Page 84: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 85: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

builder

Page 86: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

builder works via method_missing

but what about this?

Page 87: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 88: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

the problem:

how do you inherit from object...

...with inheriting from object?

Page 89: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

BlankSlate

Recorder

Jim Weirich’s BlankSlate1.8

1.9BasicObject

BasicObject

Recorder

Object

RecorderBuilder Builder

Page 90: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 91: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

abstractions with dsl’s

Groovy encapsulates and builds up

Ruby either builds

via meta-programming

different philosophies

Page 92: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

summary

Page 93: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 94: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 95: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

the real JDK™ 2.0!

unparalleled Java™ integration

willing to evolve the language

building up from best-of-breed

the good

Page 96: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

never spread out of the Java™ community

fast changing at the core

single runtime platform

framework approach?

the bad

Page 97: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

def name

public String getName()public void setName(String name)

Page 98: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 99: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

futuristic framework approach

well established (older than Java™)

virtually effortless meta-programming

interpreted, then jit-ted

truly cross platform, for the new definition of platform

the good

Page 100: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

unfamiliar frameworks

some impedance mismatch with Java™ (smaller all the time)

yet another language to learn

you have to learn to think like a Ruby-ist

you have to switch to a new community

the bad

Page 101: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform
Page 102: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

ThoughtWorks

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License.

http://creativecommons.org/licenses/by-sa/3.0/us/

?’splease fill out the session evaluationssamples at github.com/nealford

NEAL FORD software architect / meme wrangler

ThoughtWorks

[email protected] 3003 Summit Boulevard, Atlanta, GA 30319

www.nealford.com

www.thoughtworks.com

blog: memeagora.blogspot.com

twitter: neal4d

Page 103: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

resources

ThoughtWorks

groovy web sitehttp://groovy.codehaus.org

jruby sitehttp://jruby.codehaus.org

Cow in the Road photo by Sophia Huda, from Flickr, under a Creative Commons license

charles nutter’s bloghttp://headius.blogspot.com/

ola bini’s bloghttp://olabini.blogspot.com/

Page 104: Comparing Groovy and JRuby - oracle.com · Groovy dynamicizes the Java TM platform JRuby brings Ruby to the Java TM platform

resources

ThoughtWorks

graeme rocher’s bloghttp://graemerocher.blogspot.com/

aboutGroovyhttp://aboutGroovy.com

jeff brown’s bloghttp://javajeff.blogspot.com/

venkat subramaniam’s bloghttp://www.agiledeveloper.com/blog/