Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baroquebobcat/dc27b72e3387a4a60470 to your computer and use it in GitHub Desktop.
Save baroquebobcat/dc27b72e3387a4a60470 to your computer and use it in GitHub Desktop.
package cool.extensions
extension StringExt[String]
def toInt
Integer.parseInt(self)
end
end
# or, less overloaded w/ generics
extension StringExt of String
def toInt
Integer.parseInt(self)
end
end
# or flip it around
extend String with StringExt
def toInt
Integer.parseInt(self)
end
end
# - should it be self, or should we have a different id for the
# extended target?
#
# - if we have an implicit target, should fields be referred to with @ syntax?
package awesome
use_extension cool.extensions.StringExt
puts "123".toInt + 5
package awesome
extensions(cool.extensions.StringExt)
puts "123".toInt + 5
package awesome
import extension cool.extensions.StringExt
puts "123".toInt + 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment