Forum: Ruby how to find self class name inside self difinition ?

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.
C5c0e0499446b84fd3bcc61cf7862539?d=identicon&s=25 Thani Ararsu (thaniyarasu)
on 2008-11-10 06:35
module Aa
end

class Aa::Name
  def self.class_name
    self.class.name
  end
end
puts Aa::Name.class_name  # 'Class'

# But i want my class name as 'Aa::Name' instead of 'Class'


any idea ?
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2008-11-10 07:32
Thani Ararsu wrote:
> module Aa
> end
>
> class Aa::Name
>   def self.class_name
>     self.class.name
>   end
> end
> puts Aa::Name.class_name  # 'Class'
>
> # But i want my class name as 'Aa::Name' instead of 'Class'
>
>
> any idea ?

Just drop the .class part.
class Aa::Name
  def self.class_name; self.name; end # self is redundant so:
  def self.class_name; name; end # now for this we got a nice little
construct that goes:
  class << self
    alias class_name name
  end
end

Regards
Stefan
Cec345a59245af9d06e4438a413f4eb5?d=identicon&s=25 Shot (Piotr Szotkowski) (Guest)
on 2008-11-10 07:33
(Received via mailing list)
Thani Ararsu:

> module Aa
> end

> class Aa::Name
>   def self.class_name
>     self.class.name
>   end
> end
> puts Aa::Name.class_name  # 'Class'

(This is correct; the object ‘Aa::Name’ is of class ‘Class’.)

> # But i want my class name as 'Aa::Name' instead of 'Class'

You want the Module#name method:

class Aa::Name
  def self.class_name
    name
  end
end

Aa::Name.class_name # => 'Aa::Name'

-- Shot
C5c0e0499446b84fd3bcc61cf7862539?d=identicon&s=25 Thani Ararsu (thaniyarasu)
on 2008-11-10 10:28
Thanks
A57cee3563767e50b9c6462d4772b952?d=identicon&s=25 unknown (Guest)
on 2008-11-10 11:11
(Received via mailing list)
module Aa
end

class Aa::Name
  def class_name
    self.class.name
  end
end
puts Aa::Name.new().class_name
5389e59e491b2f618d0cf40acf0f2a71?d=identicon&s=25 Stephen Pearson (Guest)
on 2008-11-10 12:20
(Received via mailing list)
Thani Ararsu wrote:
> # But i want my class name as 'Aa::Name' instead of 'Class'
>
>
> any idea ?

module Aa
end

class Aa::Name
   def self.class_name
     name
   end
end
puts Aa::Name.class_name

Or dispense with the class method entirely:

puts Aa::Name.name
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-10 14:50
(Received via mailing list)
2008/11/10 Stephen Pearson <steve.6qa8e@mailnull.com>:

> Or dispense with the class method entirely:
That would be my advice as well.  Note also that Module#to_s is an
alias for Module#name so you can even print the class object directly
(if it is for output purposes).

> puts Aa::Name.name

Ruby version 1.8.7
irb(main):001:0> module Aa
irb(main):002:1> class Name
irb(main):003:2> puts name, self.name, self
irb(main):004:2> end
irb(main):005:1> end
Aa::Name
Aa::Name
Aa::Name
=> nil
irb(main):006:0>


Kind regards

robert
This topic is locked and can not be replied to.