Skip to content

Instantly share code, notes, and snippets.

@gisikw
Created April 18, 2012 01:21
Show Gist options
  • Save gisikw/2410328 to your computer and use it in GitHub Desktop.
Save gisikw/2410328 to your computer and use it in GitHub Desktop.
Dart: Ruby Example
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# The following code demonstrates how Ruby can
# interact with multiple types with a single method
# definition.
def first(arg)
arg[0]
end
# Application
arg1 = [1,2,3]
arg2 = {
:first_key => "first_val",
:second_key => "second_val",
:third_key => "third_val"
}
arg3 = "Test string"
puts first(arg1)
puts first(arg2)
puts first(arg3)
# Output:
# 1
# nil
# 84
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# This is modification of Application1.rb
# It demonstrates how Ruby can interact with
# custom types with a single method definition.
def first(arg)
arg[0]
end
class Foo
# By defining how a Foo object responds to indexing,
# we can determine what the return value will be
def [](index)
"Ruby is awesome"
end
end
# Application
arg1 = Foo.new
puts first(arg1)
# Output:
# "Ruby is awesome"
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# This ia modification of Application1.rb
# In this case, we have changed the first method to have
# a different dependency.
# Now the method definition will initially sort
# the argument, and then return the first value
def first(arg)
arg.sort[0]
end
# Application
arg1 = [1,2,3]
arg2 = {
:first_key => "first_val",
:second_key => "second_val",
:third_key => "third_val"
}
arg3 = "Test string"
puts first(arg1)
puts first(arg2)
puts first(arg3)
# Output:
# 1
# NoMethodError: undefined method `<=>' for :first_key:Symbol
# In this example, we see that while there is no
# explicit type for the first method, there are
# implicit restrictions on what it can handle.
# Code Snippet for
# Dart: A Replacement for JavaScript
# A talk given at Twin Cities Code Camp 12
# http://hasmanyreasons.com/2012/04/17/dart-why-you-should-care
# This ia modification of Application3.rb
# Now, when we try to provide our own class,
# we can see the challenges of defining an
# object that will meet the implicit requirements
# of our method.
def first(arg)
arg.sort[0]
end
class Foo
def [](index)
nil
end
# The first method will call .sort on
# our instance of Foo, so we need to
# define a method to respond.
def sort
return nil
end
end
# Application
arg1 = Foo.new
puts first(arg1)
# Output: NoMethodError: undefined method `[]' for nil:NilClass
# While we respond to sort, the return value that
# Foo provided did not respond to the [] method.
# Without any explicit warning, we have defined a
# first method that implicitly requires
# "an argument which responds to .sort with an
# object that responds to []"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment