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 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 –
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
On Oct 31, 7:34 am, kylichuku [email protected] 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@:2(irb)
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!
On Oct 31, 9:34 am, kylichuku [email protected] 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.
yes
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 10/31/07, Phrogz [email protected] wrote:
irb(main):006:0> f = Foo.new
=> #Foo:0x34f454irb(main):007:0> f.a-b
NoMethodError: undefined method `a’ for #Foo:0x34f454
from (irb):7
from :0irb(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 Oct 31, 9:56 am, “David A. Black” [email protected] wrote:
irb(main):002:0> class C
irb(main):003:1> define_method(“x-y”) { puts “Weird method” }
irb(main):004:1> endAt 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.
Hi,
On Thu, 2007-11-01 at 00:05 +0900, Brian A. wrote:
my-method called
Why?
Arlen
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 B.
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.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”
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.
ara.t.howard wrote:
‘a-b’ / foo
or similar
one more char - no hacks.
OTOH, foo.“a-b” is conservative…
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 B.
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/
On 11/1/07, Arlen Christian Mart C. [email protected] wrote:
Why?
He does not seem to be around like now.
If I see him I let him know.
R.
On 11/2/07, Giles B. [email protected] wrote:
one more char - no hacks.
(“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.
On 11/2/07, Robert D. [email protected] 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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs