Ruby On Rails 面试题与答案
Question: What are Class Variables? How to define them?Answer:Class variables are created using the @@ prefix to denote the variable as class level. It works just like any other variable, however in the case of inheritance it works more like a static variable that is accessed across all variable instances. Another example can be found here: class DemoClass @@my_var = nil def initialize @@my_var = "hello world" end def my_var puts @@my_var end end class Demo2Class < DemoClass def initialize @@my_var = "goodbye world" end end demo1 = DemoClass.new demo1.my_var demo2 = Demo2Class.new demo2.my_var demo1.my_var The output would be as shown below: hello world goodbye world goodbye world |
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
这有帮助吗? 是 否
用户评价最有帮助的内容:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?