Dynamic Methods

Hi –

In any case, what did you need this for?

I assume you mean having the class respond to methods that match a regexp?

So you can have a user input commands without having to type the whole
command. Also for having aliases for commands without having to write each
method to call the main method.

class foo
def /[ls|dir|directory]/
end
def /desc.*/
end
end

For aliasing, you can use, ummmm, alias :slight_smile:

class Foo
def ls; end
alias :dir :ls
alias :directory :ls
end

David

···

On Sun, 14 Jul 2002, John wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I’m still curious as to why you think you need this feature, however implemented.

···

— John nojgoalbyspam@hotmail.com wrote:

Basically I thought it was nigh impossible too. But you never know
in this
crazy world. I see things quite often that I didn’t know where
possible.
Ruby has a few of those!!!

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes

Or, as I said:

class Foo
def ls; end
def describe; end
def method_missing(*args)
case args[0].to_s
when /ls|dir|directory/
self.ls(args[1…args.size - 1])
when /desc.
/
self.describe(*args[1…args.size - 1])
else
raise NameError, “undefined method #{args[0].to_s}”
end
end
end

– Dossy

···

On 2002.07.14, David Alan Black dblack@candle.superlink.net wrote:

Hi –

On Sun, 14 Jul 2002, John wrote:

In any case, what did you need this for?

I assume you mean having the class respond to methods that match a regexp?

So you can have a user input commands without having to type the whole
command. Also for having aliases for commands without having to write each
method to call the main method.

class foo
def /[ls|dir|directory]/
end
def /desc.*/
end
end

For aliasing, you can use, ummmm, alias :slight_smile:

class Foo
def ls; end
alias :dir :ls
alias :directory :ls
end


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

“Dossy” dossy@panoptic.com wrote in message
news:20020713190648.GB9691@panoptic.com

In any case, what did you need this for?

I assume you mean having the class respond to methods that match a
regexp?

So you can have a user input commands without having to type the whole
command. Also for having aliases for commands without having to write
each
method to call the main method.

class foo
def /[ls|dir|directory]/
end
def /desc.*/
end
end

foo.ls
foo.dir
foo.directory

foo.desc
foo.describe

Personally I think its kinda neat. I love the power of regular
expressions.

So, implement #method_missing that delegates to the actual method name
using regex as the mapper?

– Dossy


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Yes. Thats what Daves post did, the second response to this thread.

John.

···

On 2002.07.14, John nojgoalbyspam@hotmail.com wrote:

“David Alan Black” dblack@candle.superlink.net wrote in message
news:Pine.LNX.4.30.0207131505370.9015-100000@candle.superlink.net

Hi –

In any case, what did you need this for?

I assume you mean having the class respond to methods that match a
regexp?

So you can have a user input commands without having to type the whole
command. Also for having aliases for commands without having to write
each
method to call the main method.

class foo
def /[ls|dir|directory]/
end
def /desc.*/
end
end

For aliasing, you can use, ummmm, alias :slight_smile:

class Foo
def ls; end
alias :dir :ls
alias :directory :ls
end

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Yes you can. I didn’t want aliasing though. It was just an example of what
this could be used for. I wanted to match based on a configurable regexp.

John.

···

On Sun, 14 Jul 2002, John wrote:

Ah, sorry, I asked this before I saw your next post which answered
the question. :D’oh!:

I’m still abit dubious about it other than perhaps as a curiosity.
For all but a very, very few circumstances, dynamic interfaces
haven’t seemed to really catch on anywhere other than almost pure
academia.

I find in many things, programming not the least of them, that
because you CAN do something doesn’t mean you should.

Assume you do write a class with this dynamic interface; what does
your documentation of its API look like? Often (granted, not
always), people will more appreciate being told “Here’s how to do it”
(assuming it makes sense) moreso than “here’s how to do it. Or this
way. Or any of these ways. There’s no difference though, take your
pick”.

If I had that in front of me, I’d wonder WHY there are 6 ways to do
it when 1 suffices; I’d be wondering why there are 6, and what the
difference is, and why I should choose 1 over the other 5.

But that’s me. =)

···

— John nojgoalbyspam@hotmail.com wrote:

Basically I thought it was nigh impossible too. But you never
know
in this
crazy world. I see things quite often that I didn’t know where
possible.
Ruby has a few of those!!!

I’m still curious as to why you think you need this feature,
however implemented.

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes

implemented.

I can see where he’s coming from. He wants
to allow arbitrary abbreviations in textual
commands and use the command name as the
method name (if I understand correctly).

Both are sensible, I just had never thought
of combining those features.

Reminds me of the old DEC-10 days, where
these commands (IIRC) were all the same:
DIRECTORY, DIRECTOR, DIRECTO, DIRECT,
DIREC, DIRE, DIR, DI

Bascially an arbitrary unambiguous
abbreviation. Actually, probably only the
first six characters were significant
anyway.

Why six chars?
Because that’s how many sixbit characters
will fit in a 36-bit word. Yes, they
converted back and forth between sixbit
(letters, numbers, and a little more) and
ASCII. And you guys hate Unicode so much!
But the OS needed to save space because
memory was so precious, even though the
mainframe had a whopping 448K of RAM. And
we liked it. :slight_smile: 448K… ha, bet my
cellphone has more than that now.

Hal Fulton

···

----- Original Message -----
From: “Michael Campbell” michael_s_campbell@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, July 13, 2002 3:34 PM
Subject: Re: Dynamic Methods

— John nojgoalbyspam@hotmail.com wrote:

Basically I thought it was nigh impossible too. But you never know
in this
crazy world. I see things quite often that I didn’t know where
possible.
Ruby has a few of those!!!

I’m still curious as to why you think you need this feature, however

“Hal E. Fulton” hal9000@hypermetrics.com writes:

I can see where he’s coming from. He wants to allow arbitrary
abbreviations in textual commands and use the command name as the
method name (if I understand correctly).

Of course, relying on Ruby’s dispatching to parse commands, he might
be in for a surprise the first time someone enters the command
“instance_eval” :slight_smile:

Dave

“Dave Thomas” Dave@PragmaticProgrammer.com wrote in message
news:m27kjzs5k5.fsf@zip.local.thomases.com

“Hal E. Fulton” hal9000@hypermetrics.com writes:

I can see where he’s coming from. He wants to allow arbitrary
abbreviations in textual commands and use the command name as the
method name (if I understand correctly).

Of course, relying on Ruby’s dispatching to parse commands, he might
be in for a surprise the first time someone enters the command
“instance_eval” :slight_smile:

Dave

I actually have a unique prefix for all of the commands I add dynamically.
I was aware of the risk of having people call whatever they like. :wink:

John.