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.

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

···

On Wed, 31 Oct 2007, kylichuku wrote:

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

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

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

···

On Oct 31, 7:34 am, kylichuku <Kirill.Isha...@gmail.com> wrote:
  from :0

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

···

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.

> 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

···

On 10/31/07, Phrogz <phrogz@mac.com> wrote:

On Oct 31, 7:34 am, kylichuku <Kirill.Isha...@gmail.com> wrote:

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

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.

···

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.

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

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

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/

<snip>

Why?

He does not seem to be around like now.
If I see him I let him know.
R.

···

On 11/1/07, Arlen Christian Mart Cuss <celtic@sairyx.org> wrote:
--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

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/

···

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"

--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama

ara.t.howard 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...

···

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

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

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

I cannot do that :frowning:

However if you like

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

that would be easy

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

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

R.

···

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

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

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.

···

On 11/2/07, Robert Dober <robert.dober@gmail.com> wrote:

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

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