I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
class Foo
def /bar*/
puts "Wow!"
end
end
Then I could do something like:
foo = Foo.new
foo.barchart
foo.barstweward
The regexp method name throws the interpreter off of course.
I looked at the method_missing feature to implement this. It gets part of
the problem solved, but I then don’t know what the regular expression for
the method name is.
Anyone have any ideas or pointers on where I could look?
I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
How about something like this:
class Dave
# This part implements the 'alias_regexp' thingie
@@_method_matches = {}
def self.alias_regexp(regexp, name)
@@_method_matches[regexp] = name
end
def method_missing(name, *args)
name = name.to_s
@@_method_matches.each do |regexp, target|
if regexp =~ name.to_s
return send(target, *args)
end
end
super
end
# and this demonstrates it in action
def do_it
puts "do it"
end
def dont
puts "dont"
end
alias_regexp /no.*/, :dont
alias_regexp /nyet.*/, :dont
alias_regexp /yes.*/, :do_it
end
d = Dave.new
d.no_way
d.nyet_way
d.yes_please
I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
[snip]
I looked at the method_missing feature to implement this. It gets part of
the problem solved, but I then don’t know what the regular expression for
the method name is.
Anyone have any ideas or pointers on where I could look?
Not sure exactly what you want… the method name gets passed
in, right? As a symbol?
Untested code follows – doing this from memory.
def method_missing(name,args)
if name ~ /^bar./
puts “I’m #{name.to_s}, one of the bar family.”
else
puts “I’m not a bar at all.”
end
end
Hal Fulton
···
----- Original Message -----
From: “John” nojgoalbyspam@hotmail.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 12, 2002 1:58 PM
Subject: Dynamic Methods
def method_missing(name, args)
meths = instance_methods +
private_instance_methods +
protected_instance_methods
re = Regexp.new("(#{name}.)[;]")
md = re.match(meths.join(‘;’))
send(md[1], *args) if md
end
essentially it looks at all the methods available and finds a match then
uses send to call it.
this is not tested, but should work along these lines. could also be
made a little more concise.
~transami
···
On Fri, 2002-07-12 at 12:58, John wrote:
Hi all,
I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
class Foo
def /bar*/
puts “Wow!”
end
end
Then I could do something like:
foo = Foo.new
foo.barchart
foo.barstweward
The regexp method name throws the interpreter off of course.
I looked at the method_missing feature to implement this. It gets part of
the problem solved, but I then don’t know what the regular expression for
the method name is.
Anyone have any ideas or pointers on where I could look?
instersting. with something like this, if the method_missing routine was
sophisticated enough you could have “sloppy” programming. imagine your
program still working even if you misspelled a method call. a strange
notion.
~transami
···
On Fri, 2002-07-12 at 12:58, John wrote:
Hi all,
I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
class Foo
def /bar*/
puts “Wow!”
end
end
Then I could do something like:
foo = Foo.new
foo.barchart
foo.barstweward
The regexp method name throws the interpreter off of course.
I looked at the method_missing feature to implement this. It gets part of
the problem solved, but I then don’t know what the regular expression for
the method name is.
Anyone have any ideas or pointers on where I could look?
instersting. with something like this, if the method_missing routine
was sophisticated enough you could have “sloppy”
programming. imagine your program still working even if you
misspelled a method call. a strange notion.
This reminds me of a bash feature which lets you mistype a directory
name by 1 character and still have the cd work:
I wonder if anyone has any suggestions on how to create a method that
matches its name based on a regular expression. For instance:
How about something like this:
class Dave
# This part implements the 'alias_regexp' thingie
@@_method_matches = {}
def self.alias_regexp(regexp, name)
@@_method_matches[regexp] = name
end
def method_missing(name, *args)
name = name.to_s
@@_method_matches.each do |regexp, target|
if regexp =~ name.to_s
return send(target, *args)
end
end
super
end
# and this demonstrates it in action
def do_it
puts "do it"
end
def dont
puts "dont"
end
alias_regexp /no.*/, :dont
alias_regexp /nyet.*/, :dont
alias_regexp /yes.*/, :do_it
end
d = Dave.new
d.no_way
d.nyet_way
d.yes_please
instersting. with something like this, if the method_missing routine
was sophisticated enough you could have “sloppy”
programming. imagine your program still working even if you
misspelled a method call. a strange notion.
This reminds me of a bash feature which lets you mistype a directory
name by 1 character and still have the cd work:
Pass input handling like this over to libreadline. You’ll have
almost what you want at the expense of pressing a key (tab),
in a way that users are probably much more used to, and
ambiguities will be taken care of automatically. Plus you’ll
have command history, which surely does not hurt.
If you are interested in providing command line interfaces
for your applications, have a look at my cliff' module and ecalc’ example program in the rpkg repository
(http://www.allruby.com/rpkg). If you don’t want to install
rpkg, just tell me and I’ll make a .tar.gz for cliff.
Massimiliano
···
On Sat, Jul 13, 2002 at 06:58:50AM +0900, John wrote:
My thought was that you could have aliases for methods as well as having
shorter versions:
Would it be possible to see if a regular expression intersects with another
regular expression, and if so not add the one that overlaps?
it might be possible, but damned difficult.
It’s been too long since my CS days, but something about that makes me think
it’s … not, at least not with any sort of reasonable time. But I have doubts
of my doubt, too, so…
In any case, what did you need this for? In 15 years of professional
programming (granted, not all of it OOP), I can’t think of a case where I would
have even considered this to be a solution to a problem. But I haven’t seen all
the problems there are yet, so I’m sure I’m missing a few. =)
(What do you propose if more than one possibility matches, by the
way?)
Maybe to call ALL of the methods?
Or raise TooManyMethodsError
Must admit to not having thought of that yet. Obviously calling all of them
is pretty weird, due to what order are they called in.
Would it be possible to see if a regular expression intersects with another
regular expression, and if so not add the one that overlaps?
I agree with Michael that it sounds (literally, scientifically)
impossible, though living as I do under a CS glass ceiling I don’t
know how the proof would go (if there is one) and would defer to more
learned opinion if said opinion were different. But still, impossible
or not, imagine being presented with:
/(^|blah)[^\Wc-gi-z]{2,3}/
and
/^...?([hi])\1/
and having to determine whether or not a string existed that matched
them both. “Impossible” is close enough for me
I wonder whether you could get by with a very simplified wildcard
scheme (instead of regexes) – that might be easier to do this kind of
testing with.
“Massimiliano Mirra” list@chromatic-harp.com wrote in message
news:20020716231901.D216@sphere.libero.it…
···
On Sat, Jul 13, 2002 at 06:58:50AM +0900, John wrote:
My thought was that you could have aliases for methods as well as having
shorter versions:
[ls|dir|directory]
Pass input handling like this over to libreadline. You’ll have
almost what you want at the expense of pressing a key (tab),
in a way that users are probably much more used to, and
ambiguities will be taken care of automatically. Plus you’ll
have command history, which surely does not hurt.
If you are interested in providing command line interfaces
for your applications, have a look at my cliff' module and ecalc’ example program in the rpkg repository
(http://www.allruby.com/rpkg). If you don’t want to install
rpkg, just tell me and I’ll make a .tar.gz for cliff.
Massimiliano
I have never heard of libreadline before. I did a quick search and was
inundated with hits. Where is the place to get libreadline for Ruby? I saw
something related to debian, but I have Windoze.
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!!!
Raising the too many methods error is a good solution. You can even add the
methods that it matched to the exception to let the caller decide which one
they would like from that list.
(What do you propose if more than one possibility matches, by the
way?)
Maybe to call ALL of the methods?
Or raise TooManyMethodsError
Must admit to not having thought of that yet. Obviously calling all of
them
is pretty weird, due to what order are they called in.
Would it be possible to see if a regular expression intersects with
another
···
On Sat, 13 Jul 2002, John wrote:
regular expression, and if so not add the one that overlaps?
I agree with Michael that it sounds (literally, scientifically)
impossible, though living as I do under a CS glass ceiling I don’t
know how the proof would go (if there is one) and would defer to more
learned opinion if said opinion were different. But still, impossible
or not, imagine being presented with:
/(^|blah)[^\Wc-gi-z]{2,3}/
and
/^...?([hi])\1/
and having to determine whether or not a string existed that matched
them both. “Impossible” is close enough for me
I wonder whether you could get by with a very simplified wildcard
scheme (instead of regexes) – that might be easier to do this kind of
testing with.
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.
Not to mention that the regexp is data and so can be read at runtime from a
database/file. I know that due to this layer of indirection that it will
make execution slower, but thats what faster machines are for
Would it be possible to see if a regular expression intersects with
another
regular expression, and if so not add the one that overlaps?
it might be possible, but damned difficult.
It’s been too long since my CS days, but something about that makes me
think
it’s … not, at least not with any sort of reasonable time. But I have
doubts
of my doubt, too, so…
In any case, what did you need this for? In 15 years of professional
programming (granted, not all of it OOP), I can’t think of a case where I
would
have even considered this to be a solution to a problem. But I haven’t
seen all
···
the problems there are yet, so I’m sure I’m missing a few. =)
I have never heard of libreadline before. I did a quick search and was
inundated with hits. Where is the place to get libreadline for Ruby? I saw
something related to debian, but I have Windoze.
Ah! I see. Yes, in Debian it’s just a matter of getting the libreadline-ruby
package. For Windows, you’ll have to find the libreadline DLL first (which I
guess you’ll find precompiled) and then get the ruby bindings (which I guess
you’ll have to compile). Can’t help you further as I’m not a Windows guy.
I will also take a look at your cliff module.
cliff depends on libreadline. If after getting libreadline you run into
troubles with cliff, feel free to contact me offlist, I’m all for making it
platform independent if it is not already.
Massimiliano
···
On Wed, Jul 17, 2002 at 02:27:22PM +0900, John wrote:
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.
–
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)