[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Preferred way to overwrite an object method?

Student Jr

12/6/2007 5:59:00 PM

I have an app with source code I really don't want to touch, which has
an object method I want to be able to overwrite in a certain task.
(No need to restore the function.) My current technique is as
follows:

In the application:
class C
def m
puts "barf"
end
end

In my module file:
module M
def self.included(mod)
mod.module_eval <<-EOF
def m
puts "c001c0de"
end
EOF
end
end

In the rake file:
C:send(:include, M)

Now I REALLY don't like module_eval(string). Is there a preferred
method to achieve this result?
6 Answers

Robert Dober

12/6/2007 6:35:00 PM

0

On Dec 6, 2007 7:00 PM, blogger <blogger@pierian-spring.net> wrote:
> I have an app with source code I really don't want to touch, which has
> an object method I want to be able to overwrite in a certain task.
> (No need to restore the function.) My current technique is as
> follows:
>
> In the application:
> class C
> def m
> puts "barf"
> end
> end
>
> In my module file:
> module M
> def self.included(mod)
> mod.module_eval <<-EOF
> def m
> puts "c001c0de"
> end
> EOF
> end
> end
>
> In the rake file:
> C:send(:include, M)
>
> Now I REALLY don't like module_eval(string). Is there a preferred
> method to achieve this result?
>
>

module M
def self.included target
target.send :remove_method :m
end
def m; 42 end
end

Do you like this better?
You need rescue code in case m is not defined in C BTW

Cheers
Robert

--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

Student Jr

12/6/2007 8:01:00 PM

0

Ahh, much better. I had missed remove_method. In my case, there is
no possibility of the method not being defined, but if there were, a
rescue nil would do nicely.



Bill Idgerant

11/6/2013 4:33:00 PM

0

On 11/6/2013 8:25 AM, Adam H. Kerman wrote:
> Stan Brown <the_stan_brown@fastmail.fm> wrote:
>> On Tue, 05 Nov 2013 13:14:03 -0800, jess stone wrote:
>
>>> I know that the writer's have appeared
>
>> Why oh why do people think that an apostrophe means "look out! an s
>> is following"?
>
>> Apostrophes are for possessives, dammit, not for plurals! I simply
>> cannot understand why this grade-school blunder is so common.
> ^
>
> I don't agree with your use of a hyphen there.
>
> I use 's after digits and abbreviations. It's probably wrong, but I've
> been doing it for as long as I can remember.
>

It IS wrong. The apostrophe indicates either possession ( Adam's post.)
or omitted items from a word or number. (That '70s show.--There. A TV
reference to keep this legal.) However, due to the complete failure of
the school system to teach spelling, grammar and other rudimentary
skills, millions of people now use the apostrophe to indicate plural.
TV's, CD's PVR's are everywhere, perhaps because people are guessing
that an upper case noun/abbreviation/acronym thing needs special
treatment. It doesn't. TVs, CDs, PVRs. But how then to explain and
understand the all too common appearance of plan's, Camaro's and "New
homes from the $280's" that I see in ads, emails and newspapers all over?

--
Bill Idgerant
Not a real name.

jt

11/6/2013 4:50:00 PM

0

On Wed, 06 Nov 2013 09:32:53 -0700, Bill Idgerant <fake.@fake.org> wrote:

>On 11/6/2013 8:25 AM, Adam H. Kerman wrote:

>> I use 's after digits and abbreviations. It's probably wrong, but I've
>> been doing it for as long as I can remember.
>>
>
>It IS wrong. The apostrophe indicates either possession ( Adam's post.)
>or omitted items from a word or number. (That '70s show.--There. A TV
>reference to keep this legal.) However, due to the complete failure of
>the school system to teach spelling, grammar and other rudimentary
>skills, millions of people now use the apostrophe to indicate plural.

That may be true but I think the explosion of this is another thing you can
blame on the Internet. People use so many made-up abbreviations for things
now. They often use an apostrophe to separate the s from the abbreviated
words. It may not be correct but language is an ever-changing thing. Not
always a good thing either. I can't stand to read twitter most of the time
because of it. It makes even intelligent people sound like morons.

Michael Black

11/6/2013 5:41:00 PM

0

Adam H. Kerman

11/6/2013 6:11:00 PM

0

Bill Idgerant <fake.@fake.org> wrote:
>On 11/6/2013 8:25 AM, Adam H. Kerman wrote:
>>Stan Brown <the_stan_brown@fastmail.fm> wrote:
>>>On Tue, 05 Nov 2013 13:14:03 -0800, jess stone wrote:

>>>>I know that the writer's have appeared

>>>Why oh why do people think that an apostrophe means "look out! an s
>>>is following"?

>>>Apostrophes are for possessives, dammit, not for plurals! I simply
>>>cannot understand why this grade-school blunder is so common.

>>I don't agree with your use of a hyphen there.

>>I use 's after digits and abbreviations. It's probably wrong, but I've
>>been doing it for as long as I can remember.

>It IS wrong. The apostrophe indicates either possession ( Adam's post.)
>or omitted items from a word or number. (That '70s show.--There. A TV
>reference to keep this legal.) However, due to the complete failure of
>the school system to teach spelling, grammar and other rudimentary
>skills, millions of people now use the apostrophe to indicate plural.
>TV's, CD's PVR's are everywhere, perhaps because people are guessing
>that an upper case noun/abbreviation/acronym thing needs special
>treatment. It doesn't. TVs, CDs, PVRs. But how then to explain and
>understand the all too common appearance of plan's, Camaro's and "New
>homes from the $280's" that I see in ads, emails and newspapers all over?

Numerous abbreviations require a period. No.s is wrong; No.'s looks better.
It's informal writing. In formal writing, one wouldn't use an abbreviation.