class in ruby(declaration of class in ruby)
Is this开发者_运维百科 statement enough in Ruby to create a class?
demo = Amiy.new
Will it create a class named Amiy in Ruby?
No. What this code does is create an instance (object) of the Amiy
class. To create a class you use the class
statement:
class Amiy
# ...
end
Once you've created the class then you can make an instance of it:
my_object = Amiy.new
If you want to declare a new class, you should do as Jordan said and use this syntax:
class Amiy
end
But technically you can do something like this:
Amiy = Class.new
puts "Amiy: #{(Amiy).inspect}"
instance = Amiy.new
puts "instance: #{(instance).inspect}"
Running that will give you something like this:
Amiy: Amiy
instance: #<Amiy:0xb7500b24>
精彩评论