Forum: Ruby Is Your Software Working A Little Too Predictably?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2006-03-19 13:17
(Received via mailing list)
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...
Bernhard 'elven' Stoeckner (Guest)
on 2006-03-19 13:17
(Received via mailing list)
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? :)
Simon Kröger (Guest)
on 2006-03-19 14:10
(Received via mailing list)
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
Bernhard 'elven' Stoeckner (Guest)
on 2006-03-19 14:10
(Received via mailing list)
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.
Simon Kröger (Guest)
on 2006-03-19 14:10
(Received via mailing list)
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
Joel VanderWerf (Guest)
on 2006-03-19 22:04
(Received via mailing list)
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
This topic is locked and can not be replied to.