Why Ruby doesn't allow setting a local variable using an expression containing a field reader with the same name as the local variable?
Consider the following code.
class A
attr_reader :n
def initialize
@n = 1
end
def set_local_n
n = 10
end
def using_field_reader_locally
n + 100
end
def using_field_reader_to_set_local_x
x = n + 1000
end
def using_field_reader_to_set_local_n
n = n + 1000 # This line raises a NoMethodError. Why?
end
end
Why would the commented line produce an error? Why Ruby doesn't allow setting a local variable, n
in this case, using an expression containing a field reader with the same name as the local variable? Why would all other instance methods run without any problem but using_field_reader_to_set_local_n
produce an error.
Please note that I am NOT intending to define n
locally in using_field_reader_to_set_local_n
with an expression involving the locally defined n
in set_local_n
. I am intending to define n
locally in using_field_reader_to_set_local_n
with an expressing involving the field reader of @n
. The definition of set_local_n
is for showing that n
can be defined locally while a field reader named n
exists.
ruby
add a comment |
Consider the following code.
class A
attr_reader :n
def initialize
@n = 1
end
def set_local_n
n = 10
end
def using_field_reader_locally
n + 100
end
def using_field_reader_to_set_local_x
x = n + 1000
end
def using_field_reader_to_set_local_n
n = n + 1000 # This line raises a NoMethodError. Why?
end
end
Why would the commented line produce an error? Why Ruby doesn't allow setting a local variable, n
in this case, using an expression containing a field reader with the same name as the local variable? Why would all other instance methods run without any problem but using_field_reader_to_set_local_n
produce an error.
Please note that I am NOT intending to define n
locally in using_field_reader_to_set_local_n
with an expression involving the locally defined n
in set_local_n
. I am intending to define n
locally in using_field_reader_to_set_local_n
with an expressing involving the field reader of @n
. The definition of set_local_n
is for showing that n
can be defined locally while a field reader named n
exists.
ruby
@TomLord I think you misinterpreted my intent. I am not trying to use a localn
in another method,set_local_n
, to define then
inusing_field_reader_to_set_local_n
. I intend then
in the right side ofn = n + 1000
inusing_field_reader_to_set_local_n
to be the field reader of@n
. Although I didn't useattr_accessor :n
, I usedattr_reader :n
to maken
an field reader. Only becausen
is field reader, runningusing_field_reader_locally
is possible. I put upset_local_n
merely to illustrate thatn
can be set as a local variable.
– Isaac To
Nov 14 '18 at 7:38
add a comment |
Consider the following code.
class A
attr_reader :n
def initialize
@n = 1
end
def set_local_n
n = 10
end
def using_field_reader_locally
n + 100
end
def using_field_reader_to_set_local_x
x = n + 1000
end
def using_field_reader_to_set_local_n
n = n + 1000 # This line raises a NoMethodError. Why?
end
end
Why would the commented line produce an error? Why Ruby doesn't allow setting a local variable, n
in this case, using an expression containing a field reader with the same name as the local variable? Why would all other instance methods run without any problem but using_field_reader_to_set_local_n
produce an error.
Please note that I am NOT intending to define n
locally in using_field_reader_to_set_local_n
with an expression involving the locally defined n
in set_local_n
. I am intending to define n
locally in using_field_reader_to_set_local_n
with an expressing involving the field reader of @n
. The definition of set_local_n
is for showing that n
can be defined locally while a field reader named n
exists.
ruby
Consider the following code.
class A
attr_reader :n
def initialize
@n = 1
end
def set_local_n
n = 10
end
def using_field_reader_locally
n + 100
end
def using_field_reader_to_set_local_x
x = n + 1000
end
def using_field_reader_to_set_local_n
n = n + 1000 # This line raises a NoMethodError. Why?
end
end
Why would the commented line produce an error? Why Ruby doesn't allow setting a local variable, n
in this case, using an expression containing a field reader with the same name as the local variable? Why would all other instance methods run without any problem but using_field_reader_to_set_local_n
produce an error.
Please note that I am NOT intending to define n
locally in using_field_reader_to_set_local_n
with an expression involving the locally defined n
in set_local_n
. I am intending to define n
locally in using_field_reader_to_set_local_n
with an expressing involving the field reader of @n
. The definition of set_local_n
is for showing that n
can be defined locally while a field reader named n
exists.
ruby
ruby
edited Nov 14 '18 at 7:49
Isaac To
asked Nov 14 '18 at 7:11
Isaac ToIsaac To
907
907
@TomLord I think you misinterpreted my intent. I am not trying to use a localn
in another method,set_local_n
, to define then
inusing_field_reader_to_set_local_n
. I intend then
in the right side ofn = n + 1000
inusing_field_reader_to_set_local_n
to be the field reader of@n
. Although I didn't useattr_accessor :n
, I usedattr_reader :n
to maken
an field reader. Only becausen
is field reader, runningusing_field_reader_locally
is possible. I put upset_local_n
merely to illustrate thatn
can be set as a local variable.
– Isaac To
Nov 14 '18 at 7:38
add a comment |
@TomLord I think you misinterpreted my intent. I am not trying to use a localn
in another method,set_local_n
, to define then
inusing_field_reader_to_set_local_n
. I intend then
in the right side ofn = n + 1000
inusing_field_reader_to_set_local_n
to be the field reader of@n
. Although I didn't useattr_accessor :n
, I usedattr_reader :n
to maken
an field reader. Only becausen
is field reader, runningusing_field_reader_locally
is possible. I put upset_local_n
merely to illustrate thatn
can be set as a local variable.
– Isaac To
Nov 14 '18 at 7:38
@TomLord I think you misinterpreted my intent. I am not trying to use a local
n
in another method, set_local_n
, to define the n
in using_field_reader_to_set_local_n
. I intend the n
in the right side of n = n + 1000
in using_field_reader_to_set_local_n
to be the field reader of @n
. Although I didn't use attr_accessor :n
, I used attr_reader :n
to make n
an field reader. Only because n
is field reader, running using_field_reader_locally
is possible. I put up set_local_n
merely to illustrate that n
can be set as a local variable.– Isaac To
Nov 14 '18 at 7:38
@TomLord I think you misinterpreted my intent. I am not trying to use a local
n
in another method, set_local_n
, to define the n
in using_field_reader_to_set_local_n
. I intend the n
in the right side of n = n + 1000
in using_field_reader_to_set_local_n
to be the field reader of @n
. Although I didn't use attr_accessor :n
, I used attr_reader :n
to make n
an field reader. Only because n
is field reader, running using_field_reader_locally
is possible. I put up set_local_n
merely to illustrate that n
can be set as a local variable.– Isaac To
Nov 14 '18 at 7:38
add a comment |
1 Answer
1
active
oldest
votes
If you look closely, the NoMethodError
isn't about there being no method n
, but method +
for NilClass
.
In this statement:
n = n + 1000
Ruby has to decide what n
is. As you are assigning something to it (n =
) it's not a method. If you wanted to call the n=
method, you had to specify an explicit receiver (self.n =
).
So it has to be a variable. In this case local variable.
Now to make sense of something like:
n = n
Ruby ends up assigning nil
to n
. This has to do with the internals having to put a placeholder for a variable name that is being assigned.
So when you do:
n = n + 1000
What happens is:
- Create a new local variable
n
with placeholder value (nil
). - Attempt to assign to it whatever is on the right.
- Calculate what is on the right -
nil + 1000
. nil
doesn't have a+
method so an error is raised.
To do what you wanted to do, you have to explicitly reference the instance variable for its value:
n = @n + 1000
Or explicitly call the attribute reader:
n = self.n + 1000
To me, you post seems to be be right answer. However, the interpretation ofn
on the right side the equal sign inn = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation ofn
inn + 1000
is determined byn
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?
– Isaac To
Nov 14 '18 at 8:07
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of whatn
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?
– ndnenkov
Nov 14 '18 at 8:19
4
A bare reference likefoo
could either be a receiverless argumentless message-send (e.g. equivalent toself.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver likeself.foo
or adding an argument list likefoo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed:if false then foo = 42 end; foo #=> nil
.foo
is defined as local variable, but not initialized
– Jörg W Mittag
Nov 14 '18 at 8:29
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294842%2fwhy-ruby-doesnt-allow-setting-a-local-variable-using-an-expression-containing-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you look closely, the NoMethodError
isn't about there being no method n
, but method +
for NilClass
.
In this statement:
n = n + 1000
Ruby has to decide what n
is. As you are assigning something to it (n =
) it's not a method. If you wanted to call the n=
method, you had to specify an explicit receiver (self.n =
).
So it has to be a variable. In this case local variable.
Now to make sense of something like:
n = n
Ruby ends up assigning nil
to n
. This has to do with the internals having to put a placeholder for a variable name that is being assigned.
So when you do:
n = n + 1000
What happens is:
- Create a new local variable
n
with placeholder value (nil
). - Attempt to assign to it whatever is on the right.
- Calculate what is on the right -
nil + 1000
. nil
doesn't have a+
method so an error is raised.
To do what you wanted to do, you have to explicitly reference the instance variable for its value:
n = @n + 1000
Or explicitly call the attribute reader:
n = self.n + 1000
To me, you post seems to be be right answer. However, the interpretation ofn
on the right side the equal sign inn = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation ofn
inn + 1000
is determined byn
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?
– Isaac To
Nov 14 '18 at 8:07
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of whatn
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?
– ndnenkov
Nov 14 '18 at 8:19
4
A bare reference likefoo
could either be a receiverless argumentless message-send (e.g. equivalent toself.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver likeself.foo
or adding an argument list likefoo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed:if false then foo = 42 end; foo #=> nil
.foo
is defined as local variable, but not initialized
– Jörg W Mittag
Nov 14 '18 at 8:29
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
add a comment |
If you look closely, the NoMethodError
isn't about there being no method n
, but method +
for NilClass
.
In this statement:
n = n + 1000
Ruby has to decide what n
is. As you are assigning something to it (n =
) it's not a method. If you wanted to call the n=
method, you had to specify an explicit receiver (self.n =
).
So it has to be a variable. In this case local variable.
Now to make sense of something like:
n = n
Ruby ends up assigning nil
to n
. This has to do with the internals having to put a placeholder for a variable name that is being assigned.
So when you do:
n = n + 1000
What happens is:
- Create a new local variable
n
with placeholder value (nil
). - Attempt to assign to it whatever is on the right.
- Calculate what is on the right -
nil + 1000
. nil
doesn't have a+
method so an error is raised.
To do what you wanted to do, you have to explicitly reference the instance variable for its value:
n = @n + 1000
Or explicitly call the attribute reader:
n = self.n + 1000
To me, you post seems to be be right answer. However, the interpretation ofn
on the right side the equal sign inn = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation ofn
inn + 1000
is determined byn
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?
– Isaac To
Nov 14 '18 at 8:07
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of whatn
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?
– ndnenkov
Nov 14 '18 at 8:19
4
A bare reference likefoo
could either be a receiverless argumentless message-send (e.g. equivalent toself.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver likeself.foo
or adding an argument list likefoo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed:if false then foo = 42 end; foo #=> nil
.foo
is defined as local variable, but not initialized
– Jörg W Mittag
Nov 14 '18 at 8:29
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
add a comment |
If you look closely, the NoMethodError
isn't about there being no method n
, but method +
for NilClass
.
In this statement:
n = n + 1000
Ruby has to decide what n
is. As you are assigning something to it (n =
) it's not a method. If you wanted to call the n=
method, you had to specify an explicit receiver (self.n =
).
So it has to be a variable. In this case local variable.
Now to make sense of something like:
n = n
Ruby ends up assigning nil
to n
. This has to do with the internals having to put a placeholder for a variable name that is being assigned.
So when you do:
n = n + 1000
What happens is:
- Create a new local variable
n
with placeholder value (nil
). - Attempt to assign to it whatever is on the right.
- Calculate what is on the right -
nil + 1000
. nil
doesn't have a+
method so an error is raised.
To do what you wanted to do, you have to explicitly reference the instance variable for its value:
n = @n + 1000
Or explicitly call the attribute reader:
n = self.n + 1000
If you look closely, the NoMethodError
isn't about there being no method n
, but method +
for NilClass
.
In this statement:
n = n + 1000
Ruby has to decide what n
is. As you are assigning something to it (n =
) it's not a method. If you wanted to call the n=
method, you had to specify an explicit receiver (self.n =
).
So it has to be a variable. In this case local variable.
Now to make sense of something like:
n = n
Ruby ends up assigning nil
to n
. This has to do with the internals having to put a placeholder for a variable name that is being assigned.
So when you do:
n = n + 1000
What happens is:
- Create a new local variable
n
with placeholder value (nil
). - Attempt to assign to it whatever is on the right.
- Calculate what is on the right -
nil + 1000
. nil
doesn't have a+
method so an error is raised.
To do what you wanted to do, you have to explicitly reference the instance variable for its value:
n = @n + 1000
Or explicitly call the attribute reader:
n = self.n + 1000
edited Nov 14 '18 at 7:49
answered Nov 14 '18 at 7:43
ndnenkovndnenkov
28k74879
28k74879
To me, you post seems to be be right answer. However, the interpretation ofn
on the right side the equal sign inn = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation ofn
inn + 1000
is determined byn
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?
– Isaac To
Nov 14 '18 at 8:07
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of whatn
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?
– ndnenkov
Nov 14 '18 at 8:19
4
A bare reference likefoo
could either be a receiverless argumentless message-send (e.g. equivalent toself.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver likeself.foo
or adding an argument list likefoo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed:if false then foo = 42 end; foo #=> nil
.foo
is defined as local variable, but not initialized
– Jörg W Mittag
Nov 14 '18 at 8:29
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
add a comment |
To me, you post seems to be be right answer. However, the interpretation ofn
on the right side the equal sign inn = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation ofn
inn + 1000
is determined byn
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?
– Isaac To
Nov 14 '18 at 8:07
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of whatn
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?
– ndnenkov
Nov 14 '18 at 8:19
4
A bare reference likefoo
could either be a receiverless argumentless message-send (e.g. equivalent toself.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver likeself.foo
or adding an argument list likefoo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed:if false then foo = 42 end; foo #=> nil
.foo
is defined as local variable, but not initialized
– Jörg W Mittag
Nov 14 '18 at 8:29
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
To me, you post seems to be be right answer. However, the interpretation of
n
on the right side the equal sign in n = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation of n
in n + 1000
is determined by n
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?– Isaac To
Nov 14 '18 at 8:07
To me, you post seems to be be right answer. However, the interpretation of
n
on the right side the equal sign in n = n + 1000
seems to be a bit strange to be. What you are saying is that the interpretation of n
in n + 1000
is determined by n
being in the left side of the equal sign. This way of interpreting is different than the way used in other languages I encountered. Can you elaborate in the rules for this kind of interpretation? Can you find documentation for these rules?– Isaac To
Nov 14 '18 at 8:07
1
1
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of what
n
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?– ndnenkov
Nov 14 '18 at 8:19
@IsaacTo within the same scope, the same identifier has to refer to the same thing, unless you explicitly change what it's referring to. And as evaluation is top to bottom and left to right, the attempt at assignment is encountered first. At that point the decision of what
n
is has to be made. Can you give me an example of a language where the same identifier used in the same scope refers to different things?– ndnenkov
Nov 14 '18 at 8:19
4
4
A bare reference like
foo
could either be a receiverless argumentless message-send (e.g. equivalent to self.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver like self.foo
or adding an argument list like foo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed: if false then foo = 42 end; foo #=> nil
. foo
is defined as local variable, but not initialized– Jörg W Mittag
Nov 14 '18 at 8:29
A bare reference like
foo
could either be a receiverless argumentless message-send (e.g. equivalent to self.foo()
) or a local variable dereference. Since you can easily tell Ruby that it is a message-send (by either adding a receiver like self.foo
or adding an argument list like foo()
), if there is an ambiguity, Ruby always prefers to interpret it as a local variable dereference. So, what is a local variable? A local variable is created when an assignment to a bare identifier is parsed: if false then foo = 42 end; foo #=> nil
. foo
is defined as local variable, but not initialized– Jörg W Mittag
Nov 14 '18 at 8:29
1
1
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
@ndnenkov No, I can't give you an example. My understanding of this particular aspect of a computer language was incorrect. I tried similar code in C, and the behavior was similar. Thanks for answer and explanation.
– Isaac To
Dec 20 '18 at 2:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294842%2fwhy-ruby-doesnt-allow-setting-a-local-variable-using-an-expression-containing-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@TomLord I think you misinterpreted my intent. I am not trying to use a local
n
in another method,set_local_n
, to define then
inusing_field_reader_to_set_local_n
. I intend then
in the right side ofn = n + 1000
inusing_field_reader_to_set_local_n
to be the field reader of@n
. Although I didn't useattr_accessor :n
, I usedattr_reader :n
to maken
an field reader. Only becausen
is field reader, runningusing_field_reader_locally
is possible. I put upset_local_n
merely to illustrate thatn
can be set as a local variable.– Isaac To
Nov 14 '18 at 7:38