Is Your Software Working A Little Too Predictably?

T

thoran

Hello,

Is Test::Unit always producing 0 errors?

I have the solution!

Stir in a packet of RandomMethod and "Hey Presto!", a return to the
delights of indeterminacy!

# RandomMethod mixin
# 0.0.6
# by thoran
# 20060319
# Raison D'etre: Because I can!?
# Acknowledgements: I used a bit of John W. Long's posting to ruby-talk
from 2004-07-31 for the random bit although it looks a bit different
now.
# Changes: It is within a begin-rescue block now, since no parameters
are being passed it would sometimes fail and so will now try until it
finds a method that works without parameters.

module RandomMethod
def random_method
begin
methods = self.methods
random_method = methods[rand(methods.size)]
self.send(random_method)
rescue
random_method
end
end
end

Perhaps this can be my entry into Ruby Quiz of the Week's as yet
unannounced 'quiz': 'Stupidest Ruby Code Ever Competition?' Perl has
competitions for obfuscation; how about we go for obtuseness?

Actually is there *any* good purpose to this?



thoran

P.S. I've already started thinking about the next version, or a similar
thing, which will automatically randomize methods on object creation or
method call using meta-classes...
 
B

Bernhard 'elven' Stoeckner

(e-mail address removed) scribbled on Sunday 19 Mar 2006 09:33:

module RandomMethod
def random_method
m = self.methods - Object.new.methods -
Object.methods - ["random_method"]
m = m.collect {|n|
self.method(n).arity == 0 ? self.method(n) : nil
}.compact

m[rand(m.size)].call() if m.size > 0
end
end
Perhaps this can be my entry into Ruby Quiz of the Week's as yet
unannounced 'quiz': 'Stupidest Ruby Code Ever Competition?' Perl has
competitions for obfuscation; how about we go for obtuseness?

Actually is there *any* good purpose to this?

None I can think of? :)
 
S

Simon Kröger

Bernhard said:
(e-mail address removed) scribbled on Sunday 19 Mar 2006 09:33:

module RandomMethod
def random_method
m = self.methods - Object.new.methods -
Object.methods - ["random_method"]
m = m.collect {|n|
self.method(n).arity == 0 ? self.method(n) : nil
}.compact

m[rand(m.size)].call() if m.size > 0
end
end


you call() on a string rather than on the object.
Improved version with 'usecase' :


module RandomMethod
def random_method
m = methods - Object.new.methods -
Object.methods - ["random_method"]

m.reject{|n| method(n).arity.nonzero?}
method(m[rand(m.size)]).call() if m.size > 0
end
end

class Dice
include RandomMethod
def one; 1;end
def two; 2;end
def three; 3;end
def four; 4;end
def five; 5;end
def six; 6;end
end

dice = Dice.new
10.times{puts dice.random_method}

rotfl

Simon
 
B

Bernhard 'elven' Stoeckner

Simon Kröger scribbled on Sunday 19 Mar 2006 12:21:
you call() on a string rather than on the object.

Nah :)
self.method(n).arity == 0 ? self.method(n) : nil
Im putting in Method objects in the collect block.

Your version, however, looks nicer.
 
J

Joel VanderWerf

Hello,

Is Test::Unit always producing 0 errors?

I have the solution!

Stir in a packet of RandomMethod and "Hey Presto!", a return to the
delights of indeterminacy!

Why stop there?


$DEBUG=false
$SAFE=3

# On the theory that if something goes wrong, you should just
# try something else until you find something that works.
module DuckDebugging
def method_missing(meth, *args, &block)
m = methods
meth2 = m[rand(m.size)]
result = send(meth2, *args, &block)
rescue => ex
p [self, meth2, args, ex] if $DEBUG
retry
else
p [self, meth2, args] if $DEBUG
result
end
end

class Object
include DuckDebugging
end

result = (now your program can be any batch of garbage
just enter words at random and you will get something)

p result
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top