instance-variables

Ruby - local variable gets modified when changing instance variable

半城伤御伤魂 提交于 2021-02-08 10:44:23
问题 temp gets @board.dup , and @board array is modified. However, temp gets modified as well! I have tried reading all the related documentations but still couldn't figure out an explanation. class Test def initialize @board = [[1,2],[3,4], [5,6]] end def modify temp = @board.dup #Also tried .clone print 'temp: ';p temp print '@board: ';p @board @board.each do |x| x << "x" end print "\ntemp: ";p temp print '@board: ';p @board end end x = Test.new x.modify Output: temp: [[1, 2], [3, 4], [5, 6]]

Ruby - local variable gets modified when changing instance variable

故事扮演 提交于 2021-02-08 10:43:16
问题 temp gets @board.dup , and @board array is modified. However, temp gets modified as well! I have tried reading all the related documentations but still couldn't figure out an explanation. class Test def initialize @board = [[1,2],[3,4], [5,6]] end def modify temp = @board.dup #Also tried .clone print 'temp: ';p temp print '@board: ';p @board @board.each do |x| x << "x" end print "\ntemp: ";p temp print '@board: ';p @board end end x = Test.new x.modify Output: temp: [[1, 2], [3, 4], [5, 6]]

Calling Instance Variables without @

主宰稳场 提交于 2021-02-04 21:42:09
问题 I'm new to Ruby programming and I've seen a few examples now where an instance variable is being called without the "@" symbol in front of it. I'm not sure if this because the method is being called and the instance variable is stored in the method as a result of a attr_reader in the class or if because I have the wrong understanding of instance variables. Here's an example of what I'm referring to taken from Russ Olsen's Eloquent Ruby, where the @unique array has the size method called on it

Calling Instance Variables without @

人盡茶涼 提交于 2021-02-04 21:42:06
问题 I'm new to Ruby programming and I've seen a few examples now where an instance variable is being called without the "@" symbol in front of it. I'm not sure if this because the method is being called and the instance variable is stored in the method as a result of a attr_reader in the class or if because I have the wrong understanding of instance variables. Here's an example of what I'm referring to taken from Russ Olsen's Eloquent Ruby, where the @unique array has the size method called on it

Static variables in java

北战南征 提交于 2021-01-28 23:09:54
问题 I have scenario like this public class Test{ static int a; int b; public static void main(String[] args){ Test t1 = new Test(); Test t2 = new Test(); } } What will be the variables in object t1 and object t2? As per my understanding since variable a is a static variable it will be both in object 1 and object 2. And b will be created separate copy for both the objects. But when I assign a value to variable b ie( int b=1 ) and call it like System.out.println(t1.b) , System.out.println(t2.b)

Static variables in java

久未见 提交于 2021-01-28 22:52:08
问题 I have scenario like this public class Test{ static int a; int b; public static void main(String[] args){ Test t1 = new Test(); Test t2 = new Test(); } } What will be the variables in object t1 and object t2? As per my understanding since variable a is a static variable it will be both in object 1 and object 2. And b will be created separate copy for both the objects. But when I assign a value to variable b ie( int b=1 ) and call it like System.out.println(t1.b) , System.out.println(t2.b)

Can you get the instance variable name from a class? [duplicate]

本秂侑毒 提交于 2021-01-26 19:11:24
问题 This question already has answers here : Getting the name of a variable as a string (25 answers) Getting an instance name inside class __init__() [duplicate] (10 answers) Closed 5 years ago . I understand that you should never use variable names within the program, but I am using is strictly for debug purposes and would like to convey the name of the variable to the user for readability. I have a file like this: class MyClass(object): def __init__(self): pass def foo(msg=""): debug("Called

Can you get the instance variable name from a class? [duplicate]

陌路散爱 提交于 2021-01-26 19:09:20
问题 This question already has answers here : Getting the name of a variable as a string (25 answers) Getting an instance name inside class __init__() [duplicate] (10 answers) Closed 5 years ago . I understand that you should never use variable names within the program, but I am using is strictly for debug purposes and would like to convey the name of the variable to the user for readability. I have a file like this: class MyClass(object): def __init__(self): pass def foo(msg=""): debug("Called

Can you get the instance variable name from a class? [duplicate]

大兔子大兔子 提交于 2021-01-26 19:09:15
问题 This question already has answers here : Getting the name of a variable as a string (25 answers) Getting an instance name inside class __init__() [duplicate] (10 answers) Closed 5 years ago . I understand that you should never use variable names within the program, but I am using is strictly for debug purposes and would like to convey the name of the variable to the user for readability. I have a file like this: class MyClass(object): def __init__(self): pass def foo(msg=""): debug("Called

Can you get the instance variable name from a class? [duplicate]

让人想犯罪 __ 提交于 2021-01-26 19:06:45
问题 This question already has answers here : Getting the name of a variable as a string (25 answers) Getting an instance name inside class __init__() [duplicate] (10 answers) Closed 5 years ago . I understand that you should never use variable names within the program, but I am using is strictly for debug purposes and would like to convey the name of the variable to the user for readability. I have a file like this: class MyClass(object): def __init__(self): pass def foo(msg=""): debug("Called