Beyond Ruby (RubyConf Argentina 2011)

72
Beyond Ruby Konstantin Haase

description

Ruby is the most flexible language out there, imposing no limitations on the developers, giving all the expressiveness possible. Or so we think. But there are languages pushing dynamic features and expressiveness far beyond what is possible in Ruby. Some are old, like Lisp and Smalltalk, some are just emerging, purely experimental languages, like Ioke or Newspeak. In this talk, we will take a look at some of these languages and what they can do that Ruby can't. What does it mean, to be homoiconic? How does a language without keywords work? Can I dispatch in more than one direction? And what is partial evaluation?

Transcript of Beyond Ruby (RubyConf Argentina 2011)

Page 1: Beyond Ruby (RubyConf Argentina 2011)

Beyond RubyKonstantin Haase

Page 2: Beyond Ruby (RubyConf Argentina 2011)
Page 3: Beyond Ruby (RubyConf Argentina 2011)
Page 4: Beyond Ruby (RubyConf Argentina 2011)
Page 5: Beyond Ruby (RubyConf Argentina 2011)
Page 6: Beyond Ruby (RubyConf Argentina 2011)
Page 7: Beyond Ruby (RubyConf Argentina 2011)
Page 8: Beyond Ruby (RubyConf Argentina 2011)

La verdadera pregunta es ...

Page 9: Beyond Ruby (RubyConf Argentina 2011)

¿Quién es el mejor fútbolista?

Page 10: Beyond Ruby (RubyConf Argentina 2011)

Ampliar tus horizonte.

No deje Ruby ...

... evolucionar!

Page 11: Beyond Ruby (RubyConf Argentina 2011)

¡Explora!

Page 12: Beyond Ruby (RubyConf Argentina 2011)

Por cierto ...

Page 13: Beyond Ruby (RubyConf Argentina 2011)
Page 14: Beyond Ruby (RubyConf Argentina 2011)
Page 15: Beyond Ruby (RubyConf Argentina 2011)
Page 16: Beyond Ruby (RubyConf Argentina 2011)

Vamos a empezar!

Page 17: Beyond Ruby (RubyConf Argentina 2011)

Goal: Tell you something new!

no erlang, no go, no clojure

no node.js

Page 18: Beyond Ruby (RubyConf Argentina 2011)

Full Object Orientation

Page 19: Beyond Ruby (RubyConf Argentina 2011)

Doesn't Ruby have that already?

Page 20: Beyond Ruby (RubyConf Argentina 2011)

OOP != OOP

Page 21: Beyond Ruby (RubyConf Argentina 2011)

"Objects are data and functionality."

Your high-school teacher

Page 22: Beyond Ruby (RubyConf Argentina 2011)

"Actually I made up the term object-oriented,and I can tell you I did not have C++ in mind."

Alan Kay

Page 23: Beyond Ruby (RubyConf Argentina 2011)

"OOP to me means only messaging,local retention and protection and hidingof state-process, and extreme late-binding

of all things."

Alan Kay

Page 24: Beyond Ruby (RubyConf Argentina 2011)

"OOP to me means only messaging,local retention and protection and hidingof state-process, and extreme late-binding

of all things."

Alan Kay

Page 25: Beyond Ruby (RubyConf Argentina 2011)

if respuesta == 42 "¡correcto!"else "equivocado"end

Page 26: Beyond Ruby (RubyConf Argentina 2011)

respuesta = 42 ifTrue: [ '¡correcto!' ] ifFalse: [ 'equivocado' ]

Page 27: Beyond Ruby (RubyConf Argentina 2011)

def true.if_true yieldenddef false.if_trueend

Page 28: Beyond Ruby (RubyConf Argentina 2011)

TrueClass.define_method(:if_true) { yield }FalseClass.define_method(:if_true) { }

Page 29: Beyond Ruby (RubyConf Argentina 2011)

class Falso def if_true; endend

Page 30: Beyond Ruby (RubyConf Argentina 2011)

x = Delegator.new({})x.is_a? Hash # => truecase xwhen Hash ...else failend

Page 31: Beyond Ruby (RubyConf Argentina 2011)

Don't treat your objects like data structures!

Page 32: Beyond Ruby (RubyConf Argentina 2011)

my_instance.properties[:foo][1,4] == "blah"my_instance.foo? "blah"

Page 33: Beyond Ruby (RubyConf Argentina 2011)

"OOP to me means only messaging,local retention and protection and hidingof state-process, and extreme late-binding

of all things."

Alan Kay

Page 34: Beyond Ruby (RubyConf Argentina 2011)

"OOP to me means only messaging,local retention and protection and hidingof state-process, and extreme late-binding

of all things."

Alan Kay

Page 35: Beyond Ruby (RubyConf Argentina 2011)

"A conforming processor may omit aninvocation of a method of a built-in class or

module for optimization purpose, and do thesame calculation as the method instead.

In this case, even if a program redefines the�method, the behavior of the program might notchange because the redefined method might not�

actually be invoked."

Ruby Standard (JIS X 3017)

Page 36: Beyond Ruby (RubyConf Argentina 2011)

Every built-in Ruby method could actually be akeyword.

Page 37: Beyond Ruby (RubyConf Argentina 2011)
Page 38: Beyond Ruby (RubyConf Argentina 2011)

Is Smalltalk ObjectOriented?

Page 39: Beyond Ruby (RubyConf Argentina 2011)

variable/constant access is no method send

self/super is no method send

Page 40: Beyond Ruby (RubyConf Argentina 2011)

Also...

no implementation treats ifTrue sends to aboolean as method send

constants are not late bound

Page 41: Beyond Ruby (RubyConf Argentina 2011)

Newspeak

there are only method sends

there is no global state

Page 42: Beyond Ruby (RubyConf Argentina 2011)

But I want lexical scope!

Page 43: Beyond Ruby (RubyConf Argentina 2011)

Foo = 42class Bar def foo(x) Foo + x endend

Page 44: Beyond Ruby (RubyConf Argentina 2011)

Outer = 23module Wrapper Inner = 42 class Nested def result Outer + Inner end endend

Page 45: Beyond Ruby (RubyConf Argentina 2011)

Solution: clever, bidirectional dispatch

Page 46: Beyond Ruby (RubyConf Argentina 2011)
Page 47: Beyond Ruby (RubyConf Argentina 2011)
Page 48: Beyond Ruby (RubyConf Argentina 2011)

Homoiconic

Page 49: Beyond Ruby (RubyConf Argentina 2011)

"In computer programming, homoiconicity is a property ofsome programming languages, in which the primary

representation of programs is also a data structure in aprimitive type of the language itself, from the Greek wordshomo meaning the same and icon meaning representation.

This makes metaprogramming easier than in a languagewithout this property."

Wikipedia

Page 50: Beyond Ruby (RubyConf Argentina 2011)

"In computer programming, homoiconicity is a property ofsome programming languages, in which the primary

representation of programs is also a data structure in aprimitive type of the language itself, from the Greek wordshomo meaning the same and icon meaning representation.

This makes metaprogramming easier than in a languagewithout this property."

Wikipedia

Page 51: Beyond Ruby (RubyConf Argentina 2011)

"In computer programming, homoiconicity is a property ofsome programming languages, in which the primary

representation of programs is also a data structure in aprimitive type of the language itself, from the Greek wordshomo meaning the same and icon meaning representation.

This makes metaprogramming easier than in a languagewithout this property."

Wikipedia

Page 52: Beyond Ruby (RubyConf Argentina 2011)

"In computer programming, homoiconicity is a property ofsome programming languages, in which the primary

representation of programs is also a data structure in aprimitive type of the language itself, from the Greek wordshomo meaning the same and icon meaning representation.

This makes metaprogramming easier than in a languagewithout this property."

Wikipedia

Page 53: Beyond Ruby (RubyConf Argentina 2011)

Homoiconic Languages

Lisp, Logix, Prolog, Snobol, Io, Ioke, Lego

Page 54: Beyond Ruby (RubyConf Argentina 2011)

LISP

Page 55: Beyond Ruby (RubyConf Argentina 2011)

'(1 2 3)

Page 56: Beyond Ruby (RubyConf Argentina 2011)

(+ 1 2) ; 3

Page 57: Beyond Ruby (RubyConf Argentina 2011)

(setf b 23)

Page 58: Beyond Ruby (RubyConf Argentina 2011)

(car '(1 2)) ; 1(cdr '(1 2)) ; '(2)(car (cdr '(1 2))) ; 2

Page 59: Beyond Ruby (RubyConf Argentina 2011)

(setf a '(setf b 23))(eval a)

Page 60: Beyond Ruby (RubyConf Argentina 2011)

(setf a '(setf b 23))(setf (car (cdr (cdr a))) 42)(eval a)

Page 61: Beyond Ruby (RubyConf Argentina 2011)
Page 62: Beyond Ruby (RubyConf Argentina 2011)

How's that useful?

Macros and Partial Evaluation

Language extensions from within

Page 63: Beyond Ruby (RubyConf Argentina 2011)

DeclarativeProgramming

SQL, XSLT, SPARQL, Pig

Page 64: Beyond Ruby (RubyConf Argentina 2011)

Did the last slide just say SQL and XSLT?

Seriously?

Page 65: Beyond Ruby (RubyConf Argentina 2011)

declarative programming can be fun

Prolog, Erlang

Page 66: Beyond Ruby (RubyConf Argentina 2011)

Prolog

Page 67: Beyond Ruby (RubyConf Argentina 2011)

ruby_core(tenderlove).?- ruby_core(tenderlove).Yes?- ruby_core(X).X = tenderlove

Page 68: Beyond Ruby (RubyConf Argentina 2011)

ruby_core(tenderlove).rails_core(tenderlove).ubercore(X) :- ruby_core(X), rails_core(X).?- ubercore(X).X = tenderlove

Page 69: Beyond Ruby (RubyConf Argentina 2011)

Prolog is Lisp, kinda

car([H|_], H).cdr([_|T], T).?- car([1, 2, 3], X).X = 1

?- cdr([1, 2, 3], X).X = [2, 3]

Page 70: Beyond Ruby (RubyConf Argentina 2011)

Sort a list

sorted([]).sorted([_]).sorted([X,Y|T]) :- X =< Y, sorted([Y|T]).sort(X,Y) :- perm(X,Y), sorted(Y).

Page 71: Beyond Ruby (RubyConf Argentina 2011)
Page 72: Beyond Ruby (RubyConf Argentina 2011)

Thanks!github.com / rkh / presentations