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.
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
···
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!
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 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.
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".
Ruby and monkepatching produces results on Google.
While I have no problems with working against the Establishment, I don't
feel subversive enough to confuse a relative nuby.
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.
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.
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.
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
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 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.
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?
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.
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.
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
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!)