[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Where to put code for extending a class?

Zoop Zoop

5/6/2008 8:48:00 PM

I want to extend the String class with a capitalize_each_word method
(http://donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method...)
What is the recommended place to put the code?
--
Posted via http://www.ruby-....

30 Answers

Robert Dober

5/6/2008 10:53:00 PM

0

On Tue, May 6, 2008 at 10:47 PM, Zoop Zoop <manuel.meurer@gmail.com> wrote:
> I want to extend the String class with a capitalize_each_word method
> (http://donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method...)
> What is the recommended place to put the code?
> --
> Posted via http://www.ruby-....
>
>
I would clearly monkeypatch String itself.

class String
def cap_each.....


HTH
Robert


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

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Zoop Zoop

5/7/2008 6:44:00 AM

0

Robert, could you explain a bit more what you mean?

Do you mean adding the code directly to the original string.rb file?
Where is that located?

Are there other solutions?

/M

Robert Dober wrote:
> On Tue, May 6, 2008 at 10:47 PM, Zoop Zoop <manuel.meurer@gmail.com>
> wrote:
>> I want to extend the String class with a capitalize_each_word method
>> (http://donttrustthisguy.com/2005/12/31/ruby-extending-classes-and-method...)
>> What is the recommended place to put the code?
>> --
>> Posted via http://www.ruby-....
>>
>>
> I would clearly monkeypatch String itself.
>
> class String
> def cap_each.....
>
>
> HTH
> Robert
>
>
> --
> http://ruby-smalltalk.blo...
>
> ---
> Whereof one cannot speak, thereof one must be silent.
> Ludwig Wittgenstein

--
Posted via http://www.ruby-....

Phillip Gawlowski

5/7/2008 7:27:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

A: It makes it difficult to follow the conversation
!: Why is top posting bad?

Zoop Zoop wrote:
| Robert, could you explain a bit more what you mean?

He means that you can crack open Ruby's classes when ever you want.

Adding a 'class String;end' definition anywhere in your code opens up
String, and adds your method. This is called Monkeypatching, and you'll
get fun results if several people get the same idea, and all of them
monkeypatch String.

| Do you mean adding the code directly to the original string.rb file?

No. Any source file in your application / directory structure. as long
as require can find it, you can use it.

| Are there other solutions?

Extend String with your method.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

Rule of Open-Source Programming #33:

Don't waste time on writing test cases and test scripts - your users are
your best testers.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkghWb8ACgkQbtAgaoJTgL8/EACfWzCD+XIRv4vpi6jp+LCx0RtT
mekAn2+GgDPWY6Nmyo886U01O+h2cQCu
=asV0
-----END PGP SIGNATURE-----

Zoop Zoop

5/7/2008 7:44:00 AM

0

Phillip Gawlowski wrote:
> A: It makes it difficult to follow the conversation
> !: Why is top posting bad?

Hehe, this took me a second to understand, but I got your clue.
Is in-between-posting ok?

> | Do you mean adding the code directly to the original string.rb file?
>
> No. Any source file in your application / directory structure. as long
> as require can find it, you can use it.

Ok, but where exactly is the recommended place to put code for
extensions like the one I want to do?
--
Posted via http://www.ruby-....

Damjan Rems

5/7/2008 7:57:00 AM

0

Zoop Zoop wrote:
> Phillip Gawlowski wrote:
>> A: It makes it difficult to follow the conversation
>> !: Why is top posting bad?
>
> Hehe, this took me a second to understand, but I got your clue.
> Is in-between-posting ok?
>
>> | Do you mean adding the code directly to the original string.rb file?
>>
>> No. Any source file in your application / directory structure. as long
>> as require can find it, you can use it.
>
> Ok, but where exactly is the recommended place to put code for
> extensions like the one I want to do?

Anywhere in your program flow.

Usualy you have some kind of home grown library writen by yourself,
which gets required into your application at start. There is a good
place for it.

by
TheR
--
Posted via http://www.ruby-....

Siep Korteling

5/7/2008 9:51:00 AM

0

Damjan Rems wrote:
> Zoop Zoop wrote:
>> Phillip Gawlowski wrote:
>>> A: It makes it difficult to follow the conversation
>>> !: Why is top posting bad?
>>
>> Hehe, this took me a second to understand, but I got your clue.
>> Is in-between-posting ok?
>>
>>> | Do you mean adding the code directly to the original string.rb file?
>>>
>>> No. Any source file in your application / directory structure. as long
>>> as require can find it, you can use it.
>>
>> Ok, but where exactly is the recommended place to put code for
>> extensions like the one I want to do?
>
> Anywhere in your program flow.
>
(...)
> TheR
Well, anywhere before you actually use the new method.

If you would move the class String...end block to the end of the code,
the following would not work:

class String
def capitalize_each
self.split(" ").each{|word| word.capitalize!}.join(" ")
end
def capitalize_each!
replace capitalize_each
end
end

print "Type some words here: "
str = gets
print "This is the capitalize method: "
puts str.capitalize
print "and this is the custom made capitalize_each method: "
puts str.capitalize_each
print "The original string: "
puts str
str.capitalize_each!
print "Now it's changed: "
puts str
puts "(any key to exit)"
a = gets


regards,

Siep
--
Posted via http://www.ruby-....

David A. Black

5/7/2008 10:09:00 AM

0

Hi --

On Wed, 7 May 2008, Phillip Gawlowski wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> A: It makes it difficult to follow the conversation
> !: Why is top posting bad?
>
> Zoop Zoop wrote:
> | Robert, could you explain a bit more what you mean?
>
> He means that you can crack open Ruby's classes when ever you want.
>
> Adding a 'class String;end' definition anywhere in your code opens up
> String, and adds your method. This is called Monkeypatching, and you'll

Not by everyone :-) I know I'm in the minority, but I'll put in a word
for those of us who dislike and, at least in my case, do not (and
never will) use the term "monkeypatching". It seems to me to do a very
bad job of conveying what's actually happening, which is neither
patching, in any sense that I've ever heard the term used, nor
"monkeying" (i.e., monkeying with the code, or monkeying around). All
"monkey" connotations are negative, and the issue of whether or not
its a good idea to modify existing classes in any given case is a lot
more complicated.

> get fun results if several people get the same idea, and all of them
> monkeypatch String.
>
> | Do you mean adding the code directly to the original string.rb file?
>
> No. Any source file in your application / directory structure. as long
> as require can find it, you can use it.
>
> | Are there other solutions?
>
> Extend String with your method.

Do you mean extending an individual string? That's probably my
favorite way of adding functionality to core-class objects.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Brian Marick

5/7/2008 1:07:00 PM

0


On May 7, 2008, at 5:08 AM, David A. Black wrote:
> Not by everyone :-) I know I'm in the minority, but I'll put in a word
> for those of us who dislike and, at least in my case, do not (and
> never will) use the term "monkeypatching". It seems to me to do a very
> bad job of conveying what's actually happening, which is neither
> patching, in any sense that I've ever heard the term used, nor
> "monkeying" (i.e., monkeying with the code, or monkeying around). All
> "monkey" connotations are negative, and the issue of whether or not
> its a good idea to modify existing classes in any given case is a lot
> more complicated.


David: do you have an alternate word or short phrase suitable for
using frequently? That is, something that fits in this sentence:

I got tired of that, so I
monkey-patched <classname>NSNotification</classname>
to define <methodname>[]</methodname>:


-----
Brian Marick, independent consultant
Mostly on agile methods with a testing slant
www.exampler.com, www.exampler.com/blog, www.twitter.com/marick


F. Senault

5/7/2008 1:09:00 PM

0

Le 07 mai à 15:06, Brian Marick a écrit :

> David: do you have an alternate word or short phrase suitable for
> using frequently? That is, something that fits in this sentence:
>
> I got tired of that, so I
> monkey-patched <classname>NSNotification</classname>
> to define <methodname>[]</methodname>:

Reopen(ed) ?

Fred
But not David.
--
Just a reflection Just a glimpse Just a little reminder Of all the
what abouts And all the might have Could have beens Another day Some
other way But not another reason to continue And now you're one of us
The wretched (Nine Inch Nails, The Wretched)

James Gray

5/7/2008 1:13:00 PM

0

On May 7, 2008, at 8:06 AM, Brian Marick wrote:

>
> On May 7, 2008, at 5:08 AM, David A. Black wrote:
>> Not by everyone :-) I know I'm in the minority, but I'll put in a
>> word
>> for those of us who dislike and, at least in my case, do not (and
>> never will) use the term "monkeypatching". It seems to me to do a
>> very
>> bad job of conveying what's actually happening, which is neither
>> patching, in any sense that I've ever heard the term used, nor
>> "monkeying" (i.e., monkeying with the code, or monkeying around). All
>> "monkey" connotations are negative, and the issue of whether or not
>> its a good idea to modify existing classes in any given case is a lot
>> more complicated.
>
>
> David: do you have an alternate word or short phrase suitable for
> using frequently?

Open classes or redefined?

> That is, something that fits in this sentence:
>
> I got tired of that, so I
> monkey-patched <classname>NSNotification</classname>
> to define <methodname>[]</methodname>:

I got tired of that, so I reopened NSNotification and added a [] method.

James Edward Gray II