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?

···

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

I would clearly monkeypatch String itself.

class String
   def cap_each.....

HTH
Robert

···

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-chaining/\)
What is the recommended place to put the code?
--
Posted via http://www.ruby-forum.com/\.

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

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

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

···

A: It makes it difficult to follow the conversation

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-forum.com/\.

Hi --

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

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

···

On Wed, 7 May 2008, Phillip Gawlowski wrote:

--
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.rubypal.com for details and updates!

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-forum.com/\.

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

···

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.

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

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

David A. Black 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.

I'm with David.

I can sort of understand the use of the term as the variant of Guerrilla Patching, but increasingly it gets used to mean almost any sort of run-time code alteration. I.e. it's becoming more noise than signal.

···

--
James Britt

http://www.rubyaz.org - Hacking in the Desert
http://www.jamesbritt.com - Playing with Better Toys

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-forum.com/\.

Reopen(ed) ?

Fred
But not David.

···

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

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

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?

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

···

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

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

Brian Marick 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 <classname>NSNotification</classname>
      to define <methodname></methodname>:

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

Zero G and I fell fine.

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

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

···

On Wed, May 7, 2008 at 3:06 PM, Brian Marick <marick@visibleworkings.com> wrote:
--
http://ruby-smalltalk.blogspot.com/

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

David A. Black wrote:

I'm with David.

I understand David's POV as well as Philip's, I would have said MP as
did Philip, whome I want to thank for the explanation of my short
answer when I got cut off by some work.
That is why I asked David if MP was a well known term for nubies, it
does not seem to be so.

However James, David did specifically say that he did not judge the
technique by disliking its name. For what I am concerned I think MP or
Core Class Reopening is like most other techniques, when used with
care it is a marvelous thing.

OP's usecase was a good one IMHO very consistent with String in
general, I believe that we shall judge techniques by the semantic use
we are making of them.

class String
   def cap_all_words .... end
end

does seem much better than things like

class MyClass < String
   def capitalize; #do cap_all_words here; end
end

I can sort of understand the use of the term as the variant of Guerrilla
Patching, but increasingly it gets used to mean almost any sort of run-time
code alteration. I.e. it's becoming more noise than signal.

What do you mean with code alteration here? I feel quite lost, seems
plain vanilla programming to me.
Maybe there are issues with reopening core classes ( can we call it
RCC ?) in libraries but for an application it often is the simplest
thing that works, and that is a big quality of code.

Just my 0.02c

Robert

···

On Wed, May 7, 2008 at 7:57 PM, James Britt <james.britt@gmail.com> wrote:
--
http://ruby-smalltalk.blogspot.com/

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

Hi --

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?

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.

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

···

On Wed, 7 May 2008, James Gray wrote:

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

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

--
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.rubypal.com for details and updates!

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

···

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

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

Brian Marick 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 <classname>NSNotification</classname>
> to define <methodname></methodname>:

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!)

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Phillip Gawlowski wrote:

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!)

It's a useful distinction.

···

--
James Britt

http://www.rubyaz.org - Hacking in the Desert
http://www.jamesbritt.com - Playing with Better Toys