This post originated from an RSS feed registered with Ruby Buzz
by rodney ramdas.
Original Post: Self destructing code
Feed Title: pinupgeek.com
Feed URL: http://feeds.feedburner.com/pinupgeek
Feed Description: A personal take on Ruby and Rails
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rodney ramdas
Latest Posts From pinupgeek.com
Advertisement
So _why wrote:
class Trial
def run_me
def self.run_me; raise Exception, "NO MORE." end
puts "Your trial period has ended."
end
end
So you can have your code self-destruct:
t = Trial.new
t.run_me
#=> Your trial period has ended.
t.run_me
#=> (trial):3:in `run_me': NO MORE. (Exception)
But then Scott wrote it in Io and now I find myself here:
Trial := Object clone do(
runMe := method(
self runMe = method(Exception raise("NO MORE."))
writeln("Your trial period has ended.")
)
)
And that allows an Ionist to have his code clean up after itself like so:
t := Trial clone
t.runMe #=> Your trial period has ended.
t.runMe #=> Exception: NO MORE.
And then jer shows me how we can do Object#method_missing in Io:
Object clone do(
foo := method(a, a .. " bob!")
)
B := A clone do(
foo := method(a, resend)
)
B foo("hey")
or you could use super() which does the same thing. I thought you could do it with forward() but that’s different. Not sure how it’s different yet.
Read: Self destructing code