Beyond Ruby (RubyConf Argentina 2011)

Post on 18-Dec-2014

1.535 views 3 download

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)

Beyond RubyKonstantin Haase

La verdadera pregunta es ...

¿Quién es el mejor fútbolista?

Ampliar tus horizonte.

No deje Ruby ...

... evolucionar!

¡Explora!

Por cierto ...

Vamos a empezar!

Goal: Tell you something new!

no erlang, no go, no clojure

no node.js

Full Object Orientation

Doesn't Ruby have that already?

OOP != OOP

"Objects are data and functionality."

Your high-school teacher

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

Alan Kay

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

of all things."

Alan Kay

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

of all things."

Alan Kay

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

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

def true.if_true yieldenddef false.if_trueend

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

class Falso def if_true; endend

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

Don't treat your objects like data structures!

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

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

of all things."

Alan Kay

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

of all things."

Alan Kay

"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)

Every built-in Ruby method could actually be akeyword.

Is Smalltalk ObjectOriented?

variable/constant access is no method send

self/super is no method send

Also...

no implementation treats ifTrue sends to aboolean as method send

constants are not late bound

Newspeak

there are only method sends

there is no global state

But I want lexical scope!

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

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

Solution: clever, bidirectional dispatch

Homoiconic

"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

"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

"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

"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

Homoiconic Languages

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

LISP

'(1 2 3)

(+ 1 2) ; 3

(setf b 23)

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

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

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

How's that useful?

Macros and Partial Evaluation

Language extensions from within

DeclarativeProgramming

SQL, XSLT, SPARQL, Pig

Did the last slide just say SQL and XSLT?

Seriously?

declarative programming can be fun

Prolog, Erlang

Prolog

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

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

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]

Sort a list

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

Thanks!github.com / rkh / presentations