How to find self class name inside self difinition?

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 ?

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
MovableType is why my local whisky merchant isn't going out
of business any time soon. -- Peter Corlett, hates-software

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

···

--
Posted via http://www.ruby-forum.com/\.

module Aa
end

class Aa::Name
  def class_name
    self.class.name
  end
end
puts Aa::Name.new().class_name

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 ?

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

Thanks

···

--
Posted via http://www.ruby-forum.com/.

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:

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

···

2008/11/10 Stephen Pearson <steve.6qa8e@mailnull.com>:

--
remember.guy do |as, often| as.you_can - without end