Shortcut to self.class?


#1

Without writing my own alias, is there an easy way to get the class of
the current object without calling self.class? I’m trying to call class
methods from within an instance method, and self.class.method_name is
just a little verbose for my taste. I could do type.method_name, but
that’s deprecated and not very clear. It would be nice if just class
by itself were understood in context as the method instead of the
keyword,
but since it’s not, I’m sort of stuck. Suggestions?


#2

On Mon, 19 Dec 2005, Mark J.Reed wrote:

Without writing my own alias, is there an easy way to get the class of
the current object without calling self.class? I’m trying to call class
methods from within an instance method, and self.class.method_name is
just a little verbose for my taste. I could do type.method_name, but
that’s deprecated and not very clear. It would be nice if just class
by itself were understood in context as the method instead of the keyword,
but since it’s not, I’m sort of stuck. Suggestions?

class Object
def klass
self.class
end
end

often i simply put this in specific classes. i also put this one in
alot

class Object
def singleton_class
class << self; self; end
end
end

it defintely best if you can keep this in your own code - but sometimes
putting global like this makes sense.

regards.

-a


#3

Mark J. Reed wrote:

Without writing my own alias, is there an easy way to get the class of
the current object without calling self.class? I’m trying to call class
methods from within an instance method, and self.class.method_name is
just a little verbose for my taste. I could do type.method_name, but
that’s deprecated and not very clear. It would be nice if just class
by itself were understood in context as the method instead of the keyword,
but since it’s not, I’m sort of stuck. Suggestions?

I think it’s a good idea to allow `class’ to be called from within
methods if there’s a dot after it

class.some_method # legal
class # illegal, or rather, creates a new class

I also think that `when’ should only be reserved when inside a case
statement. There’s no need to reserve words in places where they’re not
going to be used for that purpose anyway.

Cheers,
Daniel


#4

Daniel S. ha scritto:

I think it’s a good idea to allow `class’ to be called from within
methods if there’s a dot after it

class.some_method # legal
class # illegal, or rather, creates a new class

+1, but I wonder if this may be a limitation of the parser


#5

Quoting gabriele renzi removed_email_address@domain.invalid:

I think it’s a good idea to allow `class’ to be called from
within
methods if there’s a dot after it

class.some_method # legal
class # illegal, or rather, creates a new class

+1, but I wonder if this may be a limitation of the parser

You could do it with some work, I think, but it’d make the grammar
even uglier. self.class only works right now because the ‘meth’
in:

obj.meth

is treated totally differently than ‘meth’ in:

meth

Making class.meth another way to write self.class.meth would be a
bit like asking for e.g.:

[1]

on a line by itself to be an alias for

self[1]

-mental


#6

Quoting Daniel S. removed_email_address@domain.invalid:

class.some_method # legal
class # illegal, or rather, creates a new class

+1, but I wonder if this may be a limitation of the parser

Probably. I’m getting a bit fed up with the current parser, it
seems to kill off a lot of good ideas. What’s the status on that
new ANTLR grammar?

Ter’s still learning Ruby and I’m still learning ANTLR.

-mental


#7

gabriele renzi wrote:

but since it’s not, I’m sort of stuck. Suggestions?

I think it’s a good idea to allow `class’ to be called from within
methods if there’s a dot after it

class.some_method # legal
class # illegal, or rather, creates a new class

+1, but I wonder if this may be a limitation of the parser

Probably. I’m getting a bit fed up with the current parser, it seems to
kill off a lot of good ideas. What’s the status on that new ANTLR
grammar?

Cheers,
Daniel


#8

never really thought about this, but this might be a useful method:

def new
self.class.new
end

what are your thoughts?
greetings, Dirk.

2005/12/19, Mark J. Reed removed_email_address@domain.invalid:


#9

Quoting removed_email_address@domain.invalid:

+1, but I wonder if this may be a limitation of the parser

You could do it with some work, I think, but it’d make the
grammar even uglier.

Well, I take that back, sort of. You’ve got to do lookahead for <
versus << after class anyway.

I think I will have a better sense of what’s going to look
reasonable once I’ve gotten further on the subset grammar (I guess
I’ll devote some time to that tonight).

-mental