a tale of two rubies

51
A Tale of Two Rubies ...with apologies to Charles Dickens

Upload: joshua-ballanco

Post on 15-May-2015

306 views

Category:

Technology


3 download

DESCRIPTION

Slides from the talk "A Tale of Two Rubies" that I presented at the RuLu 2013 conference.

TRANSCRIPT

Page 1: A Tale of Two Rubies

A Tale of Two Rubies...with apologies to Charles Dickens

Page 2: A Tale of Two Rubies

About Me

Joshua BallancoName:

Page 3: A Tale of Two Rubies

About Me

Ankara, TurkeyLocation:

Page 4: A Tale of Two Rubies

About Me

Burnside DigitalEmployer:

Page 5: A Tale of Two Rubies

How many Ruby implementations do

you use?

Page 6: A Tale of Two Rubies

Matz’s Ruby InterpreterJRuby

Rubinius

IronRubyMacRuby

RubyMotion

MagLev Cardinal

Topaz

Page 7: A Tale of Two Rubies

What impact do alternate Rubies have

on me?

Page 8: A Tale of Two Rubies

What about fragmentation?

Page 9: A Tale of Two Rubies

A Story...Once upon a time there was a Rails app,

that needed to be ported to JRuby

• Ditch C-extensions

• Look for JRuby specific versions of gems

• Find a good app server

• Run your test suite!

Page 10: A Tale of Two Rubies

What do you do when...

ArgumentError: comparison of ActiveSupport::TimeWithZone with ActiveSupport::TimeWithZone failed

Page 11: A Tale of Two Rubies

A Side Note on Reading Code

Read Code

Page 12: A Tale of Two Rubies

A Side Note on Reading Code

Read Code!

Page 13: A Tale of Two Rubies

A Side Note on Reading Code

Read A Lot of Code

Page 14: A Tale of Two Rubies

A Side Note on Reading Code

Read Twice as Much Code as You Write

Page 15: A Tale of Two Rubies

A Side Note on Reading Code

Read Ten Times as Much Code as You Write

Page 16: A Tale of Two Rubies

A Side Note on Reading Code

Read Code

Page 17: A Tale of Two Rubies

Let’s Go Code Diving!activesupport-3.2.13/lib/active_support/time_with_zone.rb

Page 18: A Tale of Two Rubies

Let’s Go Code Diving!

Page 19: A Tale of Two Rubies

Confused yet?

When was the last time you called TimeWithZone.new anyway???

Page 20: A Tale of Two Rubies

We Have to go Deeper!

activesupport-3.2.13/lib/active_support/core_ext/time/zones.rb

activesupport-3.2.13/lib/active_support/core_ext/date_time/zones.rb

Page 21: A Tale of Two Rubies

We Have to go Deeper!

Read Code...and Comments

Page 22: A Tale of Two Rubies

So Far...

• TimeWithZone initializes @utc to a Time or DateTime

• Comparison with <=> passes through “other” to @utc’s <=> operator

Page 23: A Tale of Two Rubies

But then...

...why?

Page 24: A Tale of Two Rubies

Remember This?

Page 25: A Tale of Two Rubies

It Gets Worse...

Page 26: A Tale of Two Rubies

Remember this?

Page 27: A Tale of Two Rubies

Oh, ActiveSupport...

activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb

Page 28: A Tale of Two Rubies

Oh, ActiveSupport...

activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb

Page 29: A Tale of Two Rubies

This isn’t a Rails Talk...

Really...it’s not...

Page 30: A Tale of Two Rubies

Getting to the Heart of the Matter: JRuby

• If “other” is a Time object, compare

• Otherwise: nil

src/org/jruby/RubyTime.java

Page 31: A Tale of Two Rubies

Getting to the Heart of the Matter: MRI

Page 32: A Tale of Two Rubies

Getting to the Heart of the Matter: MRI

• If “other” is a Time object, compare

• If not, call <=> on other, passing self as the argument

• If that works, take the negation of the result

• Otherwise: nil

Page 33: A Tale of Two Rubies

So...Fix JRuby

Page 34: A Tale of Two Rubies

1 Ruby Down1 To Go

Page 35: A Tale of Two Rubies

AlwaysBeCurious

Page 36: A Tale of Two Rubies

What Classes Reverse Compare Like This?• Time

• ...

• String

• ...

• That’s it!

BUT!

Page 37: A Tale of Two Rubies

Oh, MRI...

Page 38: A Tale of Two Rubies

Break it down...

If “other” is a string, do a string comparison(in the else clause...yay)

Page 39: A Tale of Two Rubies

Break it down...

If “other” can not be converted into a string, return nil

Page 40: A Tale of Two Rubies

Break it down...

If “other” also doesn’t have its own comparison operator, return nil

Page 41: A Tale of Two Rubies

Break it down...

If “other” does have its own comparison operator, call it and return the negation of the result

BUT! ...

Page 42: A Tale of Two Rubies

Remember this?

Uhh...

Page 43: A Tale of Two Rubies

Fun With Poorly Specified Behavior

...but I thought <=> only ever returned -1, 0, or 1?

Page 44: A Tale of Two Rubies

So...Fix MRI

Page 45: A Tale of Two Rubies

fin?Not quite...

Page 46: A Tale of Two Rubies

What about...

Crap...

Page 47: A Tale of Two Rubies

Fun With Mutual Recursion

Page 48: A Tale of Two Rubies

Fun With Mutual Recursion

Page 49: A Tale of Two Rubies

Fun With Mutual Recursion

Page 50: A Tale of Two Rubies

The Moral

• When MRI was the only game in town, whatever MRI did was “Ruby”

• Re-implementing “Ruby” allows us to reconsider behaviors

• The key is communication and community

Page 51: A Tale of Two Rubies

finQuestions?