[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Create a method with name, containing illegal characters

kylichuku

10/31/2007 1:35:00 PM

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.

13 Answers

Phrogz

10/31/2007 1:55:00 PM

0

On Oct 31, 7:34 am, kylichuku <Kirill.Isha...@gmail.com> wrote:
> 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!


David A. Black

10/31/2007 1:56:00 PM

0

Brian Adkins

10/31/2007 3:02:00 PM

0

On Oct 31, 9:34 am, kylichuku <Kirill.Isha...@gmail.com> wrote:
> 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


Robert Dober

10/31/2007 3:20:00 PM

0

On 10/31/07, Phrogz <phrogz@mac.com> wrote:
> On Oct 31, 7:34 am, kylichuku <Kirill.Isha...@gmail.com> wrote:
> > 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" )
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


>
>
>
>


--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Trans

10/31/2007 3:21:00 PM

0



On Oct 31, 9:56 am, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
> On Wed, 31 Oct 2007, kylichuku wrote:
> > 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.

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.


Arlen Cuss

11/1/2007 11:25:00 AM

0

Hi,

On Thu, 2007-11-01 at 00:05 +0900, Brian Adkins wrote:
> 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




Why?

Arlen


Robert Dober

11/1/2007 11:45:00 AM

0

On 11/1/07, Arlen Christian Mart Cuss <celtic@sairyx.org> wrote:
<snip>
>Why?
He does not seem to be around like now.
If I see him I let him know.
R.
--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Giles Bowkett

11/1/2007 8:35:00 PM

0

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

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

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

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.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

ara.t.howard

11/2/2007 1:26:00 AM

0


On Oct 31, 2007, at 9:20 AM, Trans wrote:

>
> 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://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama



Joel VanderWerf

11/2/2007 6:46:00 AM

0

ara.t.howard wrote:
>
> On Oct 31, 2007, at 9:20 AM, Trans wrote:
>
>>
>> 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...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407