TheInternet.is_a?(Hash) == true
2006-08-14
While convalescing from a recent surgery, I had a flash of insight. One can view the Internet and a file system as one big, really big, Hash.
What is a Hash if not a way to access a value referenced by a key ?
What is a filename ? A key.
What is a URL ? A key.
the_internet.rb
1 require 'uri' 2 require 'open-uri' 3 require 'singleton' 4 5 # Access the Internet through a Hash-like interface 6 # Code originally written by 7 # François Beausoleil (francois@teksol.info) 8 # This code is released in the public domain. 9 class TheInternet 10 include Singleton 11 12 def [](url) 13 uri = url.respond_to?(:read) ? url : URI.parse(url) 14 uri.read 15 end 16 17 def []=(url, value) 18 # POST, PUT or whatever to URL 19 end 20 end
filesystem.rb
1 require 'singleton' 2 3 # Access the filesystem through a Hash-like interface 4 # Code originally written by 5 # François Beausoleil (francois@teksol.info) 6 # This code is released in the public domain. 7 class Filesystem 8 include Singleton 9 10 def [](path) 11 File.read(path) 12 end 13 14 def []=(path, value) 15 File.open(path, 'wb') do |f| 16 f.write(value) 17 # Could do fancy stuff here: 18 # YAMLize value, marshall it, etc. 19 end 20 end 21 end
The above allows us to do very interesting things:
1 # Copy from the Internet to a local file: 2 Filesystem.instance['teksol.info/index.html'] = 3 TheInternet.instance['http://blog.teksol.info/'] 4 5 # Given a suitable implementation of TheInternet#[]=, 6 # we could do: 7 search_results = 8 (TheInternet.instance['http://www.google.com/search'] = 9 {:hl => 'en', :q => 'rails'})
Is this rambling interesting ? Certainly. Is it useful ? You tell me !
DISCLAIMER: The Filesystem object above is full of security holes. It should be implemented in a chrooted, environment, check permissions, etc.
blog comments powered by Disqus