f



Create a method with name, containing illegal characters

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.

0
10/31/2007 1:34:47 PM
comp.lang.ruby 48885 articles. 0 followers. Post Follow

13 Replies
508 Views

Similar Articles

[PageSpeed] 52

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!


0
phrogz (454)
10/31/2007 1:54:55 PM
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.


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!

0
dblack (1323)
10/31/2007 1:56:08 PM
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


0
lojicdotcom (362)
10/31/2007 3:01:43 PM
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.blogspot.com/

0
robert.dober (2193)
10/31/2007 3:19:51 PM

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.


0
transfire (2969)
10/31/2007 3:20:32 PM
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


0
celtic (198)
11/1/2007 11:25:16 AM
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.blogspot.com/

0
robert.dober (2193)
11/1/2007 11:44:31 AM
> > 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-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/

0
gilesb (687)
11/1/2007 8:34:55 PM
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://codeforpeople.com/
--
share your knowledge.  it's a way to achieve immortality.
h.h. the 14th dalai lama



0
ara.t.howard (1140)
11/2/2007 1:26:04 AM
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

0
vjoel (2601)
11/2/2007 6:45:49 AM
> > 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...

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/

0
gilesb (687)
11/2/2007 7:28:40 AM
On 11/2/07, Giles Bowkett <gilesb@gmail.com> wrote:
> > > 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...
>
> 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.

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

0
robert.dober (2193)
11/2/2007 9:26:08 AM
On 11/2/07, Robert Dober <robert.dober@gmail.com> wrote:

> > ("hello world") : "puts" / Kernel
> 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.

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

0
robert.dober (2193)
11/2/2007 9:32:51 AM
Reply: