[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is Your Software Working A Little Too Predictably?

thoran@thoran.com

3/19/2006 8:33:00 AM

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


7 Answers

Bernhard 'elven' Stoeckner

3/19/2006 10:28:00 AM

0

thoran@thoran.com 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? :)

Simon Kröger

3/19/2006 11:21:00 AM

0

Bernhard 'elven' Stoeckner schrieb:
> thoran@thoran.com 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



Bernhard 'elven' Stoeckner

3/19/2006 11:52:00 AM

0

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.

Joel VanderWerf

3/19/2006 8:05:00 PM

0

thoran@thoran.com 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



--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Fred J. McCall

12/20/2010 4:05:00 PM

0

"conwaycaine" <conwaycaine@bellsouth.net> wrote:

>
>"Fred J. McCall" <fjmccall@gmail.com> wrote in message
>news:uubqg6dcpn152aum1326e4jvjbvn9gii7n@4ax.com...
>> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>>>"William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
>
>>>> We are on the Usenet in what is, at best, a colloquial environment.
>>>> Therefore such contractions are permissible. Spelling errors, however,
>>>> do not fall under this exception.
>>>>
>>>
>>>Do you have statistics to back up that outlandish claim???
>>>
>>
>> He just "nos" he's right, Conway...
>
>He is, as you say, truly hooked and gaffed.
>By now, he must surely be quite surly around the kitchen table.
>

Willie is a sad example of what too much of Usenet has become; a
playground for sad little wankers whose only self-validation is the
volume of their squalling, no matter how stupid they appear.

--
"Ignorance is preferable to error, and he is less remote from the
truth who believes nothing than he who believes what is wrong."
-- Thomas Jefferson

William Clark

12/20/2010 10:16:00 PM

0

In article <95vug653jl1i1hot6igt3k7rdadl3lf57r@4ax.com>,
Fred J. McCall <fjmccall@gmail.com> wrote:

> William Clark <wclark2@colnospamumbus.rr.com> wrote:
>
> >In article <uubqg6dcpn152aum1326e4jvjbvn9gii7n@4ax.com>,
> > Fred J. McCall <fjmccall@gmail.com> wrote:
> >
> >> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >>
> >> >
> >> >"William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
> >> >news:wclark2-5711CC.12175718122010@charm.magnus.acs.ohio-state.edu...
> >> >> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >> >
> >> >>> >> > More impressive than someone who cannot spell "defendant".
> >> >>> >> >
> >> >>> >> > By a long way. You are comical.
> >> >>> >>
> >> >>> >> You compose a sentence comprised of "By a long way" and you
> >> >>> >> question
> >> >>> >> Fred's
> >> >>> >> command of the English language?
> >> >>> >
> >> >>> > "It is" is implied. I see you were not an English major - or even
> >> >>> > minor.
> >> >>>
> >> >>> Not so.
> >> >>> Nothing implied.
> >> >>> Just a poorly constructed sentence.
> >> >>> The hilarious part here is that you put forth that travesty in a
> >> >>> spelling
> >> >>> flame.
> >> >>> Really, Bill, if you need resort to flame spellings, please make sure
> >> >>> you
> >> >>> adhere to the rules of grammar.
> >> >>
> >> >> We are on the Usenet in what is, at best, a colloquial environment.
> >> >> Therefore such contractions are permissible. Spelling errors, however,
> >> >> do not fall under this exception.
> >> >>
> >> >
> >> >Do you have statistics to back up that outlandish claim???
> >> >
> >>
> >> He just "nos" he's right, Conway...
> >
> >Thank you for finally acknowledging it :-)
> >
>
> Oh, I freely acknowledge that you are a legend in your own mind and
> think you "no" all about spelling and grammar and such...

Tired, Freddie, tired. Even for you.

William Clark

12/20/2010 10:17:00 PM

0

In article <5gvug6598au1dm8fadbg8s6pk3ns6q9gru@4ax.com>,
Fred J. McCall <fjmccall@gmail.com> wrote:

> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
>
> >
> >"Fred J. McCall" <fjmccall@gmail.com> wrote in message
> >news:uubqg6dcpn152aum1326e4jvjbvn9gii7n@4ax.com...
> >> "conwaycaine" <conwaycaine@bellsouth.net> wrote:
> >>>"William Clark" <wclark2@colnospamumbus.rr.com> wrote in message
> >
> >>>> We are on the Usenet in what is, at best, a colloquial environment.
> >>>> Therefore such contractions are permissible. Spelling errors, however,
> >>>> do not fall under this exception.
> >>>>
> >>>
> >>>Do you have statistics to back up that outlandish claim???
> >>>
> >>
> >> He just "nos" he's right, Conway...
> >
> >He is, as you say, truly hooked and gaffed.
> >By now, he must surely be quite surly around the kitchen table.
> >
>
> Willie is a sad example of what too much of Usenet has become; a
> playground for sad little wankers whose only self-validation is the
> volume of their squalling, no matter how stupid they appear.

And that pronouncement comes from one of the Usenet's best known, and
most discredited, trolls. What a hoot.