Obj()

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:

Hi --

···

On Thu, 27 Oct 2005, Daniel Schierbeck 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.

But they aren't.

David

--
David A. Black
dblack@wobblini.net

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

Hi --

···

On Thu, 27 Oct 2005, nobu.nokada@softhome.net wrote:

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

Please tell me that's just "experimental"... :slight_smile:

David

--
David A. Black
dblack@wobblini.net

Hi,

···

In message "Re: obj()" on Fri, 28 Oct 2005 02:41:01 +0900, "David A. Black" <dblack@wobblini.net> writes:

(obj)("a", "b") # now does work in 1.9

Please tell me that's just "experimental"... :slight_smile:

Now it's removed from 1.9

              matz.