Ruby instance variables and class instance variables

2011-04-28  来源:本站原创  分类:Ruby  人气:55 

Code first

class Kitty
  @class_ins_1 = 1

  attr_accessor :ins_1, :ins_2

  def initialize
    @ins_1 = 11
  end

  class << Kitty
    attr_accessor :class_ins_1, :class_ins_2
  end
end

puts Kitty.class_ins_1
Kitty.class_ins_2 = 2
puts Kitty.class_ins_2

k = Kitty.new
puts k.ins_1
k.ins_2 = 22
puts k.ins_2

It can be seen, the class instance variables and instance variables are essentially the same.
Only a class instance variable is an instance of the class instance variables can refer to the nature of the syntax of a tongue twister Ruby (come in under the challenge)

相关文章
  • Ruby instance variables and class instance variables 2011-04-28

    Code first class Kitty @class_ins_1 = 1 attr_accessor :ins_1, :ins_2 def initialize @ins_1 = 11 end class << Kitty attr_accessor :class_ins_1, :class_ins_2 end end puts Kitty.class_ins_1 Kitty.class_ins_2 = 2 puts Kitty.class_ins_2 k = Kitty.new put

  • java foundation's "instance methods, instance variables, class methods, class variables" 2008-10-25

    Analysis of these first 11 Dongdong: instance method, instance variables, class methods, class variables. Instance variable (instance variable): or call instance fields, instance fields (instance field), or known as member variables (member variable)

  • object references an unsaved transient instance - save the transient instance before flushing 2014-04-15

    异常1:not-null property references a null or transient value 解决方法:将"一对多"关系中的"一"方,not-null设置为false (参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212) 异常2:org.hibernate.TransientObjectException: object references

  • object references an unsaved transient instance - save the transient instance be 2014-04-15

    异常1:not-null property references a null or transient value 解决方法:将"一对多"关系中的"一"方,not-null设置为false (参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212) 异常2:org.hibernate.TransientObjectException: object references

  • How not to use intermediate variables to exchange two variables? 2010-07-10

    int, then on the + and then cut. If the connection string and then intercept. int a, b; a = a + b; / / At this point a to a and b of and b = ab; / / now b = a a = ab; / / then a = b

  • To talk about Ruby variables 2009-03-11

    School at the time heard that dynamic languages, python La. , ruby, lua you are. Get a picture of the object can change the runtime state, such as to add property to increase the Ways La. . But dynamic languages has been in the end do not know how to

  • Ruby variables constant brief 2010-02-23

    1: The variable is a placeholder for an object or reference. For example: x=10 puts 10 Here is the 10 assigned to the variable named x 2: The variable name must be a single entity, without spaces. Must be a letter or underscore at the beginning, must

  • ruby class methods instance methods 2010-08-14

    Ruby has just contacted shortly after that Ruby is also really fun. In the learning process, found a more special place in Ruby: class methods and instance methods. In Ruby, class methods and instance methods are different, Ruby class object class me

  • The scope of JavaScript variables do not block 2010-10-31

    Coverage of local variables global variables, as follows: <script> // Define global variables test var test = " Global variables "; // Defined function checkscope function checkscope() { // Definition of local variables // var test = "

  • java static variables 2010-03-30

    Sometimes you want to define a class member, use it completely independent of any object class. Usually cases, class members must pass it the class of object access, but you can create such a member, it can be it Ziji used without having to reference

  • Comparison of various variables stored speed 2010-04-17

    Look at the basic knowledge under public class Variable{ static int allClicks=0;// Class variables String str="hello world";// Instance variables \ Personally, I think when you create the instance Initialization Initializes the assignment before

  • FMS in the class. Methods and variables 2010-04-23

    It seems to sum up their frequent problems encountered in peacetime, they are not to every time google. Google a few days ago to a good article, results in a few days to see when that article has been unable to find, and it seems better to write arti

  • Instance constructor is not a static method? 2010-04-25

    As title. The answer depends on your heart "static" to refer to something. See a recent forum posts: renpeng301 writes If you are not familiar with the JVM instructions, see the things that really difficult to understand a very intuitive default

  • Database name. Instance name. ORACLE_SID. Database name. Global database name. Service name 2010-05-11

    First, the database name database name is a database of identity, as identity card numbers of people, he said with a parameter DB_NAME. When you create the database should be considered a good database name, and create a complete database, the databa

  • ORACLE database name. Instance name. ORACLE_SID difference 2010-05-13

    Database name (DB_NAME), instance name (Instance_name), and operating system environment variables (ORACLE_SID) In ORACLE7, 8 database, only the database name (db_name) and the database instance name (instance_name). In ORACLE8i, 9i in the emergence

  • Use ADO wrapper class instance of the database application development (under) 2010-07-12

    Use ADO wrapper class instance of the database application development (under) Original Address: http://www.vckbase.com/document/viewdoc/?id=611 In "Using ADO wrapper class instance of the database application development (on)" ADO described in

  • DB2 database creation and management of the server instance 2010-07-21

    DB2 database creation and management of the server instance 1: The DB2 instance is an instance of the implementation of the concept of code and database objects in the middle of the logical layer, all of the database and its objects is a logical coll

  • oracle database terms (database name, instance name, the network service name and service name, etc.) 2010-10-12

    <! - [If gte mso 9]> <xml> <w:WordDocument> <w:BrowserLevel> MicrosoftInternetExplorer4 </ w: BrowserLevel> <w:DisplayHorizontalDrawingGridEvery> 0 </ w: DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDra

  • C | C + + in a static global variables, static local variables, global variables, local variables, the difference between 2010-10-22

    There are two static usage: process-oriented programming, object-oriented design of static and the static. The former applies to ordinary variables and functions, does not involve classes; latter description of the role of static in the class. Static

  • Static method and instance method of the difference (to) 2010-10-24

    Static method and instance method differences This is a frequently raised issue from time to time, a lot of times we think that understanding, and understand, but upon closer look, we find and understand. The way we write every day, many programmers