# Is Your Software Working A Little Too Predictably?

**URL:** <https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299>\
**Category:** Ruby\
**Created:** [March 19, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299 "2006-03-19T12:17:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/1 "2006-03-19T12:17:08Z")

</div>

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 Q. 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…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/2 "2006-03-19T12:17:11Z")

</div>

removed\_email\_address@domain.invalid 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 Q. 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? 🙂

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/3 "2006-03-19T13:10:29Z")

</div>

Bernhard ‘elven’ Stoeckner schrieb:

> ```
> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/4 "2006-03-19T13:10:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/5 "2006-03-19T13:10:35Z")

</div>

Bernhard ‘elven’ Stoeckner schrieb:

> Simon KrÃ¶ger scribbled on Sunday 19 Mar 2006 12:21:
> 
> > you call() on a string rather than on the object.
> 
> Nah 🙂

Sorry, my fault.

[method(n) for n in m if method(n).arity == 0]

oops, wrong language 🙂

> > self.method(n).arity == 0 ? self.method(n) : nil  
> > Im putting in Method objects in the collect block.
> 
> Your version, however, looks nicer.

cheers

Simon

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 19, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/is-your-software-working-a-little-too-predictably/55299/6 "2006-03-19T21:04:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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
