[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method switching

Lee Jarvis

9/5/2007 4:24:00 PM

Ok this probably won't make any send at all, but lets say i have
something like this:

def send(a)
puts "#{a}\n"
end

def colorsend(a)
puts "#{colorize(a)}\n"
end

def something
send "hello"
end



Ok lets say i have 50+ methods like the something method, that relate to
the send method, i want to be able to have 1 switch to change the method
without changing every method that uses send(message)
for example i could use

if @color = 1
colorsend(msg)
else
send(msg)
end

but that means i would have to add that to *every* method.. if anyone at
all out there understands what i mean it would be very helpful

thanks in advance..
--
Posted via http://www.ruby-....

7 Answers

Robert Klemme

9/5/2007 4:44:00 PM

0

2007/9/5, Lee Jarvis <jarvo88@gmail.com>:
> Ok this probably won't make any send at all, but lets say i have
> something like this:
>
> def send(a)
> puts "#{a}\n"
> end
>
> def colorsend(a)
> puts "#{colorize(a)}\n"
> end
>
> def something
> send "hello"
> end
>
>
>
> Ok lets say i have 50+ methods like the something method, that relate to
> the send method, i want to be able to have 1 switch to change the method
> without changing every method that uses send(message)
> for example i could use
>
> if @color = 1
> colorsend(msg)
> else
> send(msg)
> end
>
> but that means i would have to add that to *every* method.. if anyone at
> all out there understands what i mean it would be very helpful

Since you are dispatching based on an instance variable, why don't you
wrap send and colorsend in another method that does the dispatching
and invoke this other method from your 50+ methods?

Btw, send is a bad name to choose because this is a standard method.

I also wonder why you have 50+ methods that are somehow similar. It
seems there is room for improvement. Maybe you can state the business
problem you are trying to solve.

Cheers

robert

Lee Jarvis

9/5/2007 4:57:00 PM

0

Robert Klemme wrote:
> Since you are dispatching based on an instance variable, why don't you
> wrap send and colorsend in another method that does the dispatching
> and invoke this other method from your 50+ methods?

how so?

> Btw, send is a bad name to choose because this is a standard method.

Yeah i know.. it's not actually send, that was just an example


> I also wonder why you have 50+ methods that are somehow similar.

Who said they were similar?

They all print different data to the console.. I just want to give the
user the choice, /SET COLOR=1 or something similar and the rest of the
data becomes colored..

Maybe i should not be printing data from every different method, if i
was only returning data then i suppose i could control the output type
in one method..

Thanks for the help also..
--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/5/2007 5:24:00 PM

0

Lee Jarvis wrote:
> Ok this probably won't make any send at all, but lets say i have
> something like this:
>
> def send(a)
> puts "#{a}\n"
> end
>
> def colorsend(a)
> puts "#{colorize(a)}\n"
> end
>
> def something
> send "hello"
> end
>
>
>
> Ok lets say i have 50+ methods like the something method, that relate to
> the send method, i want to be able to have 1 switch to change the method
> without changing every method that uses send(message)

You could just redefine the method in question, like this:

def color_off()
def my_print(a)
puts a
end
end

def color_on()
def my_print(a)
puts colorize(a)
end
end

def something()
my_print "hello"
end

color_off()
something() #no color
color_on()
something() #color


HTH,
Sebastian
--
NP: Ulver - Dressed in Black
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sebastian Hungerecker

9/5/2007 5:30:00 PM

0

Sebastian Hungerecker wrote:
> Lee Jarvis wrote:
> > Ok this probably won't make any send at all, but lets say i have
> > something like this:
> >
> > def send(a)
> > puts "#{a}\n"
> > end
> >
> > def colorsend(a)
> > puts "#{colorize(a)}\n"
> > end
> >
> > def something
> > send "hello"
> > end
> >
> >
> >
> > Ok lets say i have 50+ methods like the something method, that relate to
> > the send method, i want to be able to have 1 switch to change the method
> > without changing every method that uses send(message)
>
> You could just redefine the method in question

Or you could, of course, just check some var inside inside of the method, like

def send(a)
if @color
puts colorize(a)
else
puts a
end
end

Or did I totally misunderstand what you're trying to do?


--
NP: Arcturus - Ad Absurdum
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

9/5/2007 5:38:00 PM

0

2007/9/5, Lee Jarvis <jarvo88@gmail.com>:
> Robert Klemme wrote:
> > Since you are dispatching based on an instance variable, why don't you
> > wrap send and colorsend in another method that does the dispatching
> > and invoke this other method from your 50+ methods?
>
> how so?

def wrap_send(m)
if @color = 1
colorsend(m)
else
send(m)
end
end

def some_method
...
wrap_send("foo")
...
end

> > Btw, send is a bad name to choose because this is a standard method.
>
> Yeah i know.. it's not actually send, that was just an example
>
>
> > I also wonder why you have 50+ methods that are somehow similar.
>
> Who said they were similar?

It seemed so since all of them need that printing to be done. You
might get better results when refactoring your code.

> They all print different data to the console.. I just want to give the
> user the choice, /SET COLOR=1 or something similar and the rest of the
> data becomes colored..
>
> Maybe i should not be printing data from every different method, if i
> was only returning data then i suppose i could control the output type
> in one method..

Maybe. I can't tell because I still have too little information.

> Thanks for the help also..

ywc

Kind regards

robert

Lee Jarvis

9/5/2007 5:54:00 PM

0

> def wrap_send(m)
> if @color = 1
> colorsend(m)
> else
> send(m)
> end
> end
>

Ahh crap i didn't even think of doing that, that's exactly what i have
done now, sorry for the time wasting, noobish mistake on my behalf..

Thanks a lot guys
--
Posted via http://www.ruby-....

Gunner Asch

11/14/2012 4:33:00 PM

0


Federalizing the Election Process [Excerpts]
OK Politechs | 11/13/12 | Charles M. Phipps

On election day there were long lines in some states and many people
waited hours to vote.

There have already been calls by some in Congress (all Democrats, so
far) to have Congress intervene to "normalize" voting nationwide and
avoid the long delays of last Tuesday.

Eliminating lines and streamlining the voting process sounds great,
but elections are run by the states and the federal government has no
authority to step in. The notion that these are federal elections is
a misnomer because all but the president and vice president represent
individual states or districts. Even the vote for president is not
really a vote for the candidate, but for the electors from the state
who will vote for that candidate at a later date.

And although it has not been mentioned by proponents of federalizing
the election process, I believe an eventual goal of doing so would be
the elimination of the Electoral College. There are many people who
believe we should elect the president on a popular vote. This also
goes against the intentions of the Founders and the federal system
they created.

The national popular vote is a misguided movement that ignores the
original intent of the Founders when they drafted the Constitution.
The federal coalition of the United States was designed with the
intent that the states, not the direct vote of the people, would
select the President.

The United States was created as a republic; not a democracy.

A Republic is representative government ruled by law (the
Constitution).

A Democracy is direct government ruled by the majority (mob rule).

A Republic recognizes the inalienable rights of individuals while
democracies are only concerned with group wants or needs (the public
good).

Lawmaking is a slow, deliberate process in our Constitutional Republic
requiring approval from the House, Senate, Executive (President or
Governor), The Supreme Court, and individual jurors.

Lawmaking in our unlawful democracy occurs rapidly requiring approval
from the whim of the majority as determined by polls and/or voter
referendums.

Democracies always self-destruct when the non-productive majority
realizes that it can vote itself handouts from the productive minority
by electing the candidate promising the most benefits from the public
treasury. To maintain their power, these candidates must adopt an
ever-increasing tax and spend policy to satisfy the ever-increasing
desires of the majority. As taxes increase, incentive to produce
decreases, causing many of the once productive to drop out and join
the non-productive. When there are no longer enough producers to fund
the legitimate functions of government and the socialist programs, the
democracy will collapse, always to be followed by a dictatorship.

The Founders knew full well the differences between a Republic and a
Democracy. They repeatedly and emphatically said that they had founded
a Republic.

Article IV Section 4, of the Constitution "guarantees to every state
in this union a Republican form of government"... Conversely, the word
Democracy is not mentioned even once in the Constitution.


Our military training manuals used to contain the correct definitions
of Democracy and Republic. The following comes from Training Manual
No. 2000-25 published by the War Department, November 30, 1928.

DEMOCRACY:

A government of the masses.
Authority derived through mass meeting or any other form of
"direct" expression.
Results in mobocracy.
Attitude toward property is communistic--negating property rights.
Attitude toward law is that the will of the majority shall
regulate, whether is be based upon deliberation or governed by
passion, prejudice, and impulse, without restraint or regard to
consequences.
Results in demogogism, license, agitation, discontent, anarchy.

REPUBLIC:

Authority is derived through the election by the people of public
officials best fitted to represent them.
Attitude toward law is the administration of justice in accord
with fixed principles and established evidence, with a strict regard
to consequences.
A greater number of citizens and extent of territory may be
brought within its compass.
Avoids the dangerous extreme of either tyranny or mobocracy.
Results in statesmanship, liberty, reason, justice, contentment,
and progress.
Is the "standard form" of government throughout the world.

The manuals containing these definitions were ordered destroyed
without explanation by President Franklin D. Roosevelt early in his
administration.


A classic American movie example of "Republic vs.Democracy" is the
town sheriff holding a prisoner for trial against a lynch mob. The
Sheriff represents "Rule of Law"; the Lynch Mob "Majority Rule".


The methodology of the left has always been:

1. Lie
2. Repeat the lie as many times as possible
3. Have as many people repeat the lie as often as possible
4. Eventually, the uninformed believe the lie
5. The lie will then be made into some form oflaw
6. Then everyone must conform to the lie