These are the slides from my RubyConf 2011 talk: "Keeping Ruby Reasonable". In this talk, I covered how first-class environments are implemented in Ruby, and why that poses a problem for Ruby programmers.
24. foo = Bar.new(baz.qux)
What does it mean?!?
• Math equation?
• Program?
• What language?
25. foo = Bar.new(baz.qux)
What does it mean?!?
• Math equation?
• Program?
• What language?
• Tokens? Symbols?
26. foo = Bar.new(baz.qux)
What does it mean?!?
• Math equation?
• Program?
• What language?
• Tokens? Symbols?
• Function bindings? Semantics? Value
transformations???
34. Motivation
I really liked first-class environments when I was first
exposed to them...
...At this point I believe that first-class environments
are useless at best, and dangerous at worst.
35. Design of First-Class Environments
Four questions to ask when designing a first-
class environment implementation...
36. Design of First-Class Environments
Should you be able to extract an environment
from any closure?
1. Yes
2. No
37. Design of First-Class Environments
Should you be able to extract an environment
from any closure?
1. Yes Ruby’s Choice
2. No
38. Design of First-Class Environments
Should all bindings in the lexical scope of the
closure be captured by the environment?
1. All
2. Only those used
3. Only those explicitly specified
39. Design of First-Class Environments
Should all bindings in the lexical scope of the
closure be captured by the environment?
1. All Ruby’s Choice
2. Only those used
3. Only those explicitly specified
40. Design of First-Class Environments
Should bindings be live in the environment?
1. All bindings are live and mutable
2. Bindings are live, but read-only
3. Bindings are a snapshot in time
4. User specifies state of each binding
41. Design of First-Class Environments
Should bindings be live in the environment?
1. All bindings are live and mutable Ruby’s
2. Bindings are live, but read-only Choice
3. Bindings are a snapshot in time
4. User specifies state of each binding
42. Design of First-Class Environments
What happens if you define a new binding
using the environment?
1. New binding shadows old binding
2. Environment is cloned containing new
binding
3. Defining new bindings is not allowed
43. Design of First-Class Environments
What happens if you define a new binding
using the environment?
Ruby’s
1. New binding shadows old binding
Choice
2. Environment is cloned containing new
binding
3. Defining new bindings is not allowed
44. Design of First-Class Environments
“When someone suggests ‘first-class
environments’, I assume they want options 1, 1,
1, and 1 ... In this variation, though, the user
simply cannot reason about his code.”
45. Design of First-Class Environments
“When someone suggests ‘first-class
environments’, I assume they want options 1, 1,
1, and 1 ... In this variation, though, the user
simply cannot reason about his code.”
Ruby chooses options 1, 1, 1, and 1 !
46. What’s a language to do?
R6RS Scheme (http://www.r6rs.org/)
• Bindings in the environment must be
specified explicitly using a library reference
• New bindings cannot be created in the
environment
47. What’s a language to do?
Objective-C doesn’t have first-class environments, but...
48. What’s a language to do?
Objective-C doesn’t have first-class environments, but...
Retained, read-only binding
49. What’s a language to do?
Objective-C doesn’t have first-class environments, but...
Retained, read-only binding
Live binding
50. What’s a language to do?
Objective-C doesn’t have first-class environments, but...
Collected!
Retained, read-only binding
Live binding
51. I am but a humble Ruby programmer
• How many times have you done this?
52. I am but a humble Ruby programmer
• How many times have you done this?
• ...and thought you were safe?
53. I am but a humble Ruby programmer
• How many times have you done this?
• And later needed to do this?
54. I am but a humble Ruby programmer
• How many times have you done this?
• And later needed to do this?
55. I am but a humble Ruby programmer
• How many times have you done this?
• And later needed to do this?
• Is that even reasonable?
56. First-class Environments are hard...let’s play
a game!
Let’s play: Capture the Flag
Rules:
• You must provide a class
• The class must contain a method named “flag”
• The class must contain a class constant named “Base”
• The “Base” must be a proc object
• You may only define one class
• You may not require or load other source files in
your class
66. What did we learn?
• Ruby’s first-class environments make it
difficult to reason about code (and
therefore difficult to optimize)
• Ruby does have a restricted version of a
first-class environment in the Method class
• Re-thinking and formalizing first-class
environment semantics could make Ruby
faster, unbound methods more useful, and
Ruby more reasonable in general