Where to put code for extending a class?

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

On Tue, May 6, 2008 at 10:47 PM, Zoop Z. [email protected]
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-chaining/)
What is the recommended place to put the code?

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

I would clearly monkeypatch String itself.

class String
def cap_each…

HTH
Robert


http://ruby-smalltalk.blogspot.com/


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

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 D. wrote:

On Tue, May 6, 2008 at 10:47 PM, Zoop Z. [email protected]
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-chaining/)
What is the recommended place to put the code?

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

I would clearly monkeypatch String itself.

class String
def cap_each…

HTH
Robert


http://ruby-smalltalk.blogspot.com/


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

Phillip G. 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?

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

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

Zoop Z. 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 G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

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

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

Damjan R. wrote:

Zoop Z. wrote:

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

Hi –

On Wed, 7 May 2008, Phillip G. wrote:

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

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

On May 7, 2008, at 5:08 AM, David A. Black wrote:

Not by everyone :slight_smile: 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>:

Zoop Z. wrote:

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

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

“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 G. II

Le 07 mai à 15:06, Brian M. 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.

Hi –

On Wed, 7 May 2008, James G. wrote:

“monkey” connotations are negative, and the issue of whether or not

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.

Yes, or: I added a [] method to NSNotification.

It’s not crucial that it always be exactly the same formula, though,
as there are several ways to say it that are clear. I think settling
on any one term is a solution in search of a problem, and the use of
“monkeypatching” is a problem created by a solution in search of a
problem :slight_smile:

David

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

David A. Black wrote:

| Not by everyone :slight_smile: 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”.

Ruby and monkepatching produces results on Google. :stuck_out_tongue:

While I have no problems with working against the Establishment, I don’t
feel subversive enough to confuse a relative nuby. :wink:

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

That’s one option. Or inheriting from String into MyString, and work
from there, if it is supposed to be ‘globally’ available.

Depends on the implementation details, really.


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ - You know you’ve been hacking too long when…
…after days with YACC, you start to parse your conversations.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkghu8gACgkQbtAgaoJTgL+LMwCgm5qTURv6FDJJ6imtOkzLNZZI
DM8An3EEip66ZBzeds8OPZXioaZTnGbe
=XwAC
-----END PGP SIGNATURE-----

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

Brian M. wrote:

|
| 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 NSNotification
| to define []:

While I’m not David, nor do I play him (or any David) on TV, here’s my
two cents:

‘I reopened NSNotification and added/defined [] for use.’

Short, sweet, to the point, and neutral in its connotation.

(I admit that I chose ‘monkeypatching’ earlier to actually invoke the
negative connotations. Reopening a class shouldn’t be done lightly!)


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

Zero G and I fell fine.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkghvGoACgkQbtAgaoJTgL95PwCfeojWk88brMcDohi2NSll7akB
nRkAnRqWiGZn+68dOncw3HoNkQ2gMVAP
=7C0g
-----END PGP SIGNATURE-----

On 7-May-08, at 10:27 AM, Phillip G. wrote:

| I got tired of that, so I

(I admit that I chose ‘monkeypatching’ earlier to actually invoke the
negative connotations. Reopening a class shouldn’t be done lightly!)

I am a little confused, and maybe my understanding of Ruby classes is
muddled:

If classes are never closed then why do the need to be (re-)opened?

‘I added [] to NSNotification’

Mike

(These days I spend more time fighting SharePoint or Outlook than
doing anything useful or interesting, so if I am out of line please
forgive me.)

Mike S. removed_em[email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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

Mike S. wrote:

|
| I am a little confused, and maybe my understanding of Ruby classes is
| muddled:
|
| If classes are never closed then why do the need to be (re-)opened?

The intention is to make clear that the functionality was added after
the initial creator considered the original implementation complete (for
varying degrees of ‘complete’. It also carries the information that it
was done outside of the original implementation.

| ‘I added [] to NSNotification’

This could be taken as a patch to NSNotification’s original source code.

Using ‘reopening’ implies that it is done during runtime, rather than
during design-time (as murky as that is with Ruby).


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ “My ethicator machine must’ve had a built-in moral compromise
~ spectral release phantasmatron! I’m a genius!” — Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkghwJIACgkQbtAgaoJTgL8OVgCeNZ7HG11zt6CtJ1a2iEF9aFQO
rq0An0Ol282XDEgEVQm8gWkZjWRx1sKC
=2Hk9
-----END PGP SIGNATURE-----

Perhaps if you don’t like the term monkeypatching, you’d prefer
ninjapatching.
http://avdi.org/devblog/2008/04/01/announcing-ninja-patching/

No connotations of bad code, or screwing around! And it gets the idea
across.
–Kyle

What is the recommended place to put the code?

Personally I think it would be nice is if all extensions of standard
ruby classes etc… would go into a special .rb file.

This way people all can look at what is changed (IF they are interested)
quickly.
Not sure what name would be a common ground, “core_extensions.rb” or
“extensions.rb” or … I dont know. But having it in one file is, I
think, better in the long run.

On May 7, 2008, at 5:08 AM, David A. Black wrote:

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.

To their credit, monkeys are rather playful. And they do use their
tails, unlike some dogs I know.

Oh and the JavaScript engine in Mozilla is called SpiderMonkey. I
guess that will haunt them forever.

_why

On Wed, May 7, 2008 at 3:06 PM, Brian M.
[email protected] wrote:
Well David at first I wanted to disagree with you, because it is just
such a nice word ;), you see MP is quite ambiguous in the UK ;), but I
guess you have some reason to be against the word.
I cannot share the negative feeling with the word monkey, after all we
are close relatives, but I guess the most important question is the
following, and I am asking the teacher here:

Do you have the experience that the expression MP is confusing for
beginners?

Cheers
Robert

http://ruby-smalltalk.blogspot.com/


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs