开发者

class in ruby(declaration of class in ruby)

开发者 https://www.devze.com 2023-01-04 19:25 出处:网络
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

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消