Jay McGavren's Journal

2008-09-17

More experiments with objects that do weird crap when placed in ordinary situations…

This one lets you wrap an object so that its apparent value changes over time.

class ValueProxy
	self.instance_methods.reject{|m| m.to_str == '__id__' or m.to_str == '__send__'}.each do |method|
		undef_method(method)
	end
	def initialize(value)
		@value = value
	end
	def method_missing(method, *arguments)
		value.send(method, *arguments)
	end
end

Then we subclass it to do various things with @value

class ReverseProxy < ValueProxy
	def value
		@value.reverse
	end
end

And ordinary calls like this suddenly get weird.

string = ReverseProxy.new('foobar')
puts string
string2 = string + 'baz'
puts string2
puts ReverseProxy.new([1, 2, 3])

Output:

raboof
raboofbaz
3
2
1

A different implementation of #value

class IncrementingProxy < ValueProxy
	def value
		@value = @value.succ
	end
end

number = IncrementingProxy.new(1)
puts number
puts number
letter = IncrementingProxy.new('a')
puts letter
puts letter

…gives different results.

3
5
c
e

Other stuff I wanna try:

Numeric whose value increases by the number of seconds since its creation. Range that iterates over its elements in random order. Numeric that always returns negative of itself. Boolean that returns opposite of itself. Boolean that alternates between returning true and false.

comments powered by Disqus