[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Exploring Metaprogramming

Logan Capaldo

9/4/2006 3:32:00 AM


On Sep 3, 2006, at 10:39 PM, Michael Gorsuch wrote:

>
> What if I want to build a second method called 'command'? It
> should place
> the argument in an array that can be referenced later. It will
> need to also
> build an initialize method for my class, but one of them will have
> to be
> overwritten.
>
> Does anyone have any smart ways to combine or chain these two
> initialize
> methods together?
>
> I ultimately want to do something like this:
>
> class Dragon < Creature
> traits :life
> life 100
>
> command :fly
>
> def fly
> puts "I am flying..."
> end
>
> end
>
> After an instance is created, it will contain the instance variable
> 'commands', which is an array holding the symbol 'fly'.
>
> Does anyone have any ideas? In the end, I'd be adding more and
> more of
> these commands to my DSL. This is a DSL, right? ;-)

Sorry I seem to have under estimated what you wanted.

try this instead:

% cat dragon.rb
class Creature
def self.command(cmd)
old_initialize = instance_method(:initialize)
define_method(:initialize) do |*args|
@commands ||= []
@commands |= [cmd]
old_initialize.bind(self).call(*args)
end
end

def commands
@commands ||= []
end
end

class Dragon < Creature
command :fly
def fly
puts "I'm flying"
end

command :breathe_fire

def breathe_fire
puts "I'm breathing fire!"
end
end

dragon = Dragon.new
p dragon.commands

% ruby dragon.rb
[:breathe_fire, :fly]



2 Answers

Dave K

6/11/2011 9:32:00 PM

0

On Sat, 11 Jun 2011 14:40:23 -0500, Figaro <figaro@nevermind.com>
wrote:

>Sorry, Dave, but you couldn't be more wrong: they "get it" all too well.

I think you are giving them more credit than they deserve.

>These deviates know exactly what they're doing and why. They just don't
>give a damn about the harm they do.

I'd say that might be correct about Buzz, but I cannot imagine
Wally, or Ray, being that clever.

> They're knee-jerk fanatics with
>absolutely no conscience or sense of responsibility. They're like
>trained seals who perform on command, always grateful for any fish their
> masters deign to throw them as a reward for following orders.
>Without them fascist-totalitarian regimes couldn't succeed.

I believe the totalitarian/fascists to be comprised of two parts;
dumb people like Ray and Wally, and others that stand to profit. The
dumb ones go along thinking that the right wing fascists/totalitarians
will share the wealth with them because they supported them. The
smart ones think... Never mind, there have never been smart ones that
supported totalitarian/fascist regimes; only opportunists that
thought, and I use the term loosely, that they would profit from it.

The facts are that, historically speaking, totalitarian/fascist
regimes do not last very long. I suppose their supporters are all
really dumb that way.
--

Cheers! :)

WaIIy

6/11/2011 9:42:00 PM

0

On Sat, 11 Jun 2011 14:40:23 -0500, Figaro <figaro@nevermind.com> wrote:

>On 6/11/11 2:16 PM, Dave K wrote:
>> On Sat, 11 Jun 2011 13:20:16 -0500, Figaro<figaro@nevermind.com>
>> wrote:
>>
>>> Oh the humanity! Zombies disrupt (attack) helpless special needs
>>> children! Never mind that officials from the Special Olympics have gone
>>> on record saying that these "zombies", standing silently with their
>>> backs to the Walker whore, did NOT disrupt their ceremony. Never mind
>>> that these "thugs" were mostly students protesting Walker's plan to
>>> slash $900 million from the state's education budget and cripple Badger
>>> Care, Wisconsin's Medicare program, two acts which would harm special
>>> needs children far more than any "disruption"! Wally's video NEVER
>>> IDENTIFIES THE PROTESTERS AS UNION MEMBERS!
>>>
>>> The whole phony shit storm was orchestrated with the help of one of the
>>> more repellent Koch goons calling herself Vicky McKenna (real last name:
>>> Pyzynski) who claims to be the voice of Americans for Prosperity,
>>> a Koch-backed anti-labor, pro-corporate right-wing think tank. This
>>> douchebag, complete with police record, urged on the crowd, saying it
>>> would be a good thing to "pile on" the demonstrators and "punch them
>>> out" even if it meant being arrested!
>>> http://4.bp.blogspot.com/--ke4HejCSXc/TfJ_yXreziI/AAAAAAAAJpY/PJbvl6JwyPo/s400/wi-vicki-...
>>>
>>> And by the way, that YouTube video Wally points to was supplied courtesy
>>> of the MacIver Institute, a corporate front organization with close ties
>>> to the Koch-backed Americans for Prosperity think tank.
>>> It's disreputable record can be read at http://tinyurl.c...
>>>
>>> And so we leave Wally to weep, moan, and wring his hands over
>>> children he really cares about...as long as they help feed his wingnut
>>> mania. Buzz? He's just along for the ride, probably taking time off from
>>> being some corporate big shot's caddy.
>>
>> You will never get them to see the errors of their, "thinking." They
>> just really don't get it.
>
>Sorry, Dave, but you couldn't be more wrong: they "get it" all too well.
>These deviates know exactly what they're doing and why. They just don't
>give a damn about the harm they do. They're knee-jerk fanatics with
>absolutely no conscience or sense of responsibility. They're like
>trained seals who perform on command, always grateful for any fish their
> masters deign to throw them as a reward for following orders.
>Without them fascist-totalitarian regimes couldn't succeed.
>>

Yes you guys, we have "errors of thinking" because we point out the
obvious.

JHC, you people are fucked up.