Forum: Ruby shortcut to self.class ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark J.Reed (Guest)
on 2005-12-19 05:36
(Received via mailing list)
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?
unknown (Guest)
on 2005-12-19 05:48
(Received via mailing list)
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
Daniel S. (Guest)
on 2005-12-19 15:35
(Received via mailing list)
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
gabriele renzi (Guest)
on 2005-12-19 21:03
(Received via mailing list)
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
Daniel S. (Guest)
on 2005-12-19 21:18
(Received via mailing list)
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
unknown (Guest)
on 2005-12-19 22:07
(Received via mailing list)
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
unknown (Guest)
on 2005-12-19 22:23
(Received via mailing list)
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
Dirk M. (Guest)
on 2005-12-19 22:26
(Received via mailing list)
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>:
unknown (Guest)
on 2005-12-19 22:29
(Received via mailing list)
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
This topic is locked and can not be replied to.