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
self.class.name
end
end
puts Aa::Name.class_name # ‘Class’
any idea ?
Thani A. wrote:
module Aa
endclass 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
Thani A.:
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
module Aa
end
class Aa::Name
def class_name
self.class.name
end
end
puts Aa::Name.new().class_name
Thanks
Thani A. 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
2008/11/10 Stephen P. [email protected]:
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs