Create a method with name, containing illegal characters

K

kylichuku

Hi there!

I need to create a method with name, that contains '-' character. Is
it possible, and if the answer is "yes", how can I do it?

Thanks.
 
P

Phrogz

I need to create a method with name, that contains '-' character. Is
it possible, and if the answer is "yes", how can I do it?

lim2:~ phrogz$ irb
irb(main):001:0> class Foo
irb(main):002:1> define_method("a-b") do
irb(main):003:2* puts "What a strange need!"
irb(main):004:2> end
irb(main):005:1> end
=> #<Proc:0x00354328@(irb):2>

irb(main):006:0> f = Foo.new
=> #<Foo:0x34f454>

irb(main):007:0> f.a-b
NoMethodError: undefined method `a' for #<Foo:0x34f454>
from (irb):7
from :0

irb(main):008:0> f.send( "a-b" )
What a strange need!
 
D

David A. Black

Hi --

Hi there!

I need to create a method with name, that contains '-' character. Is
it possible, and if the answer is "yes", how can I do it?

The only way I know of is:

irb(main):002:0> class C
irb(main):003:1> define_method("x-y") { puts "Weird method" }
irb(main):004:1> end

At which point, the only way to call it is:

irb(main):005:0> C.new.send("x-y") # Weird method

In other words, it's not worth the trouble and you should find some
other solution.


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
B

Brian Adkins

Hi there!

I need to create a method with name, that contains '-' character. Is
it possible, and if the answer is "yes", how can I do it?

Thanks.

1) yes

2)

brian@imagine:~/temp$ cat > a.lisp
(defun my-method ()
(format t "my-method called"))

(my-method)
brian@imagine:~/temp$ clisp a.lisp
my-method called
 
R

Robert Dober

lim2:~ phrogz$ irb
irb(main):001:0> class Foo
irb(main):002:1> define_method("a-b") do
irb(main):003:2* puts "What a strange need!"
irb(main):004:2> end
irb(main):005:1> end
=> #<Proc:0x00354328@(irb):2>

irb(main):006:0> f = Foo.new
=> #<Foo:0x34f454>

irb(main):007:0> f.a-b
NoMethodError: undefined method `a' for #<Foo:0x34f454>
from (irb):7
from :0

irb(main):008:0> f.send( "a-b" )
Cool I did not know one could do this
What a strange need!
Not strange at all, how often did I
gsub("-","_") in my DSLs

Cheers
Robert
 
T

Trans

Hi --




The only way I know of is:

irb(main):002:0> class C
irb(main):003:1> define_method("x-y") { puts "Weird method" }
irb(main):004:1> end

At which point, the only way to call it is:

irb(main):005:0> C.new.send("x-y") # Weird method

In other words, it's not worth the trouble and you should find some
other solution.

Reminds we, I've thought this notation might be interesting in place
of send:

foo."a-b"

But I think it "scares" poeople. But I'm not sure it need to. What
kind of thing can come it? Perhaps a more literate programming style?

str."captialize every other letter"

Of course, that's really not much different than

str.captialize_every_other_letter

But, it does simplify:

item = "word"
str."captialize every other #{item}"

Furthermore, I wonder if we could go also blanket classes with
definitions for as many reasonable phrases applicatable. Can Ruby, or
any language for that matter, handle 1000s of methods per class?

T.
 
G

gilesb

I need to create a method with name, that contains '-' character. Is
The only way I know of is:

irb(main):002:0> class C
irb(main):003:1> define_method("x-y") { puts "Weird method" }
irb(main):004:1> end

At which point, the only way to call it is:

irb(main):005:0> C.new.send("x-y") # Weird method

In other words, it's not worth the trouble and you should find some
other solution.

just a tangent, Jay Fields did something cool with define_method:

http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html

he created a method called not. of course if you do

def not
# ...
end

Ruby complains about a syntax error. So there's a very useful use case
for define_method - you can only define not using define_method - even
though the original poster's question was both difficult to do and
difficult to use, so I agree with David that in that case it's not
worth the effort.

However according to Ezra Z. define_method is slower than "def," both
for definition and invocation, so for performance, you might choose
not to use define_method except in cases like Not.

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/
 
A

ara.t.howard

Reminds we, I've thought this notation might be interesting in place
of send:
foo."a-b"

js does that. i don't think it's worth it though when you can just do

alias_method '[]', 'send'

foo['a-b']

another alternative is tweaking string

class String
def /(obj) obj.send self end
end

'a-b' / foo

or similar

one more char - no hacks.

a @ http://codeforpeople.com/
 
V

vjoel

ara.t.howard said:
Reminds we, I've thought this notation might be interesting in place
of send:
foo."a-b"

js does that. i don't think it's worth it though when you can just do

alias_method '[]', 'send'

foo['a-b']

another alternative is tweaking string

class String
def /(obj) obj.send self end
end

'a-b' / foo

or similar

one more char - no hacks.

OTOH, foo."a-b" is conservative...
 
G

gilesb

another alternative is tweaking string
OTOH, foo."a-b" is conservative...

but if you could find a way to add args in a totally counter-intuitive way, like

(args) : "a-b" / foo

then you could drive your co-workers completely insane. tell them it
was a Prolog dialect you hacked together in your spare time and see if
they believe it.

("hello world") : "puts" / Kernel

I have to say, that's the most elegantly useless code I've seen in a
good long while.

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/
 
R

robert.dober

but if you could find a way to add args in a totally counter-intuitive way, like

(args) : "a-b" / foo

then you could drive your co-workers completely insane. tell them it
was a Prolog dialect you hacked together in your spare time and see if
they believe it.

("hello world") : "puts" / Kernel
I cannot do that :(

However if you like

[ "Hi," , "he said", Kernel ] <= :puts

that would be easy

class Array
def <= message
pop.send message, *self
end
end

HTHN ;)
Robert
BTW my coworkers are already insane, no work to be done there.

R.
 
R

robert.dober

I cannot do that :(
You cannot indeed, but the following is pretty close:

class Symbol # and/or String
def / object
object.method self
end
end
class Array
def <= method
method.call *self
end
end

[ "Hi," , "he said" ] <= :puts / Kernel

actually I *like* this, am I insane?

R.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top