Skip to content

Instantly share code, notes, and snippets.

@kalenp
Last active November 8, 2022 17:39
Show Gist options
  • Save kalenp/35d68a7de8c6baaeec82dadce0b22900 to your computer and use it in GitHub Desktop.
Save kalenp/35d68a7de8c6baaeec82dadce0b22900 to your computer and use it in GitHub Desktop.
Demonstration of adapting ruby methods with kwargs to kotlin interface
# cache.rb
# Existing ruby cache implementation. Uses kwargs
class Cache
include RubyCache
def set(key, value, ttl:)
...
end
end
// RubyCache.kt
// Java interface to Ruby cache
interface RubyCache {
fun set(key: String, value: Object, kwargs: Map<String, Object>)
}
// KotlinCache.kt
// Idiomatic kotlin interface for cache access
interface KotlinCache {
fun set(key: String, value: Object, ttl: Duration?)
}
// RubyCacheAdapter.kt
// Adapts RubyCache interface to KotlinCache interface
class CacheAdapter(private val rubyCache: RubyCache): KotlinCache {
override fun set(key: String, value: Object, ttl: Duration?) {
rubyCache.set(key, value, mapOf("ttl" to ttl?.seconds))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment