Would this be possible:
class Klass
def self.[] (*args)
new(*args)
end
end
# these should do the same
Klass("foo", "bar") # new
Klass["foo", "bar"] # current
obj = proc { |a, b| puts a + b }
# these should do the same
obj("a", "b") # new
obj["a", "b"] # current
It does have some complications, but it is a way to make Proc's seem like normal methods.
Just an idea
Daniel
At least the first is already possible:
class Klass
def initialize(*args)
@args = args
end
def self. (*args)
new(*args)
end
end
def Klass(*args)
Klass.new(*args)
end
# these should do the same
p Klass("foo", "bar") # new
p Klass["foo", "bar"] # current
regards,
Brian
···
On 27/10/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
Would this be possible:
class Klass
def self. (*args)
new(*args)
end
end
# these should do the same
Klass("foo", "bar") # new
Klass["foo", "bar"] # current
obj = proc { |a, b| puts a + b }
# these should do the same
obj("a", "b") # new
obj["a", "b"] # current
It does have some complications, but it is a way to make Proc's seem
like normal methods.
Just an idea
Daniel
--
http://ruby.brian-schroeder.de/
Stringed instrument chords: http://chordlist.brian-schroeder.de/
Hi,
At Thu, 27 Oct 2005 23:52:07 +0900,
Daniel Schierbeck wrote in [ruby-talk:162931]:
obj = proc { |a, b| puts a + b }
# these should do the same
obj("a", "b") # new
obj["a", "b"] # current
(obj)("a", "b") # now does work in 1.9
···
--
Nobu Nakada
Would this be possible:
class Klass
def self. (*args)
new(*args)
end
end
The usual idiom I've seen (and used) is:
class Klass
class<<self
alias : :new
end
end
# these should do the same
Klass("foo", "bar") # new
You can already do this:
def Klass(*args, &block)
Klass.new(*args, &block)
end
See the discussion starting ruby-talk/160664.
obj = proc { |a, b| puts a + b }
# these should do the same
obj("a", "b") # new
I think this is already in 1.9
Regards,
Sean
···
On 10/27/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
Is this short hand for:
obj.call("a", "b")
I assume the parens are required to avoid interpreting
the code as
self.obj("a", "b")
Gary Wright
···
On Oct 27, 2005, at 10:58 AM, nobu.nokada@softhome.net wrote:
(obj)("a", "b") # now does work in 1.9