keyongtech


  keyongtech > ruby

 #1  
05-06-08, 08:47 PM
Zoop Zoop
I want to extend the String class with a capitalize_each_word method
(http://donttrustthisguy.com/2005/12/...thod-chaining/)
What is the recommended place to put the code?
 #2  
05-06-08, 10:52 PM
Robert Dober
On Tue, May 6, 2008 at 10:47 PM, Zoop Zoop <manuel.meurer> wrote:
> I want to extend the String class with a capitalize_each_word method
> ([..])
> What is the recommended place to put the code?
> --
> Posted via [..].
>I would clearly monkeypatch String itself.


class String
def cap_each.....


HTH
Robert
 #3  
05-07-08, 06:44 AM
Zoop Zoop
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:
[..]
 #4  
05-07-08, 07:26 AM
Phillip Gawlowski
-----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.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.
 #5  
05-07-08, 07:44 AM
Zoop Zoop
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?
 #6  
05-07-08, 07:56 AM
Damjan Rems
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
 #7  
05-07-08, 09:50 AM
Siep Korteling
Damjan Rems wrote:
> Zoop Zoop wrote:
>
> 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
 #8  
05-07-08, 10:08 AM
David A. Black
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
 #9  
05-07-08, 01:06 PM
Brian Marick
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
 #10  
05-07-08, 01:09 PM
F. Senault
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.
 #11  
05-07-08, 01:13 PM
James Gray
On May 7, 2008, at 8:06 AM, Brian Marick wrote:

>
> On May 7, 2008, at 5:08 AM, David A. Black wrote:
>> 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
 #12  
05-07-08, 02:05 PM
David A. Black
Hi --

On Wed, 7 May 2008, James Gray wrote:

> On May 7, 2008, at 8:06 AM, Brian Marick wrote:
>> Open classes or redefined?
>> 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
 #13  
05-07-08, 02:25 PM
Phillip Gawlowski
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

Ruby and monkepatching produces results on Google. :P

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.

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.
 #14  
05-07-08, 02:27 PM
Phillip Gawlowski
-----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!)

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

Zero G and I fell fine.
 #15  
05-07-08, 02:38 PM
Mike Stok
On 7-May-08, at 10:27 AM, 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!)



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

Similar Threads
Extending a .NET class

Is it possible to extend/drive an existing .NET class using PowerShell and override public methods? Thanks

Extending a class

I want to create a new class derrived from TComponentList. I want to basically a few fields to the items property (meaning to access these I will need to another property...

Extending class to be a base class *properly*

Sorry if this has been discussed before (I'm almost certain it has), but I didn't know what to google for. My problem is, I have a class, a gtkmm widget, and I want it to...

Extending a class.

In order to implement a Right Mouse Button click handler on a CTreeView is am required to derive my own class from the CTreeView class. (According to an example on...


All times are GMT. The time now is 07:02 PM. | Privacy Policy