obj()

D

Daniel Schierbeck

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
 
B

Brian Schröder

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 =3D 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 =3D 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
 
N

nobu.nokada

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
 
S

Sean O'Halpin

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 =3D 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
 
G

gwtmp01

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

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
 
D

David A. Black

Hi --

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"... :)


David
 
D

David A. Black

Hi --

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: obj()"

|> (obj)("a", "b") # now does work in 1.9
|
|Please tell me that's just "experimental"... :)

Now it's removed from 1.9

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top