Fixnum object does not change value after being increased
I just started learning Ruby, and encounterd this 2 functions:
def increase(n)
n = n + 1
return n
end
def add_element(array, item)
array << item
end
When I tried increase(n) with n = 5
c = 5
p10.increase(c)
print("c is #{c}n")
print("c.class is #{c.class}n")
--> c is 5
--> c.class is Fixnum
Value of c does not change after being increased inside increase(n)
When I tried to change the content of an array arr = [1,2,3,4] with add_element, arr does change.
arr = [1, 2, 3, 4]
p10.add_element(arr, 5)
print("array is #{arr}n")
--> array is [1, 2, 3, 4, 5]
So if everything in Ruby is object, why arr changes its value, but c ( a Fixnum object ) does not change its value?
Your thought is appreciated. :) Thanks
ruby object addition fixnum
add a comment |
I just started learning Ruby, and encounterd this 2 functions:
def increase(n)
n = n + 1
return n
end
def add_element(array, item)
array << item
end
When I tried increase(n) with n = 5
c = 5
p10.increase(c)
print("c is #{c}n")
print("c.class is #{c.class}n")
--> c is 5
--> c.class is Fixnum
Value of c does not change after being increased inside increase(n)
When I tried to change the content of an array arr = [1,2,3,4] with add_element, arr does change.
arr = [1, 2, 3, 4]
p10.add_element(arr, 5)
print("array is #{arr}n")
--> array is [1, 2, 3, 4, 5]
So if everything in Ruby is object, why arr changes its value, but c ( a Fixnum object ) does not change its value?
Your thought is appreciated. :) Thanks
ruby object addition fixnum
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of1+2could be anything.
– steenslag
Nov 25 '18 at 16:54
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
What isp10? .
– sawa
Nov 26 '18 at 4:43
add a comment |
I just started learning Ruby, and encounterd this 2 functions:
def increase(n)
n = n + 1
return n
end
def add_element(array, item)
array << item
end
When I tried increase(n) with n = 5
c = 5
p10.increase(c)
print("c is #{c}n")
print("c.class is #{c.class}n")
--> c is 5
--> c.class is Fixnum
Value of c does not change after being increased inside increase(n)
When I tried to change the content of an array arr = [1,2,3,4] with add_element, arr does change.
arr = [1, 2, 3, 4]
p10.add_element(arr, 5)
print("array is #{arr}n")
--> array is [1, 2, 3, 4, 5]
So if everything in Ruby is object, why arr changes its value, but c ( a Fixnum object ) does not change its value?
Your thought is appreciated. :) Thanks
ruby object addition fixnum
I just started learning Ruby, and encounterd this 2 functions:
def increase(n)
n = n + 1
return n
end
def add_element(array, item)
array << item
end
When I tried increase(n) with n = 5
c = 5
p10.increase(c)
print("c is #{c}n")
print("c.class is #{c.class}n")
--> c is 5
--> c.class is Fixnum
Value of c does not change after being increased inside increase(n)
When I tried to change the content of an array arr = [1,2,3,4] with add_element, arr does change.
arr = [1, 2, 3, 4]
p10.add_element(arr, 5)
print("array is #{arr}n")
--> array is [1, 2, 3, 4, 5]
So if everything in Ruby is object, why arr changes its value, but c ( a Fixnum object ) does not change its value?
Your thought is appreciated. :) Thanks
ruby object addition fixnum
ruby object addition fixnum
asked Nov 25 '18 at 14:41
dia099dia099
41
41
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of1+2could be anything.
– steenslag
Nov 25 '18 at 16:54
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
What isp10? .
– sawa
Nov 26 '18 at 4:43
add a comment |
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of1+2could be anything.
– steenslag
Nov 25 '18 at 16:54
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
What isp10? .
– sawa
Nov 26 '18 at 4:43
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of
1+2 could be anything.– steenslag
Nov 25 '18 at 16:54
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of
1+2 could be anything.– steenslag
Nov 25 '18 at 16:54
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
What is
p10? .– sawa
Nov 26 '18 at 4:43
What is
p10? .– sawa
Nov 26 '18 at 4:43
add a comment |
1 Answer
1
active
oldest
votes
There are "special" objects in Ruby that are not mutable. Fixnum is one of them (others are booleans, nil, symbols, other numerics). Ruby is also pass by value.
n = n + 1 does not modify n, it reassigns a local variable in increase's scope.
Since Fixnum isn't mutable, there is no method you could use to change its value, unlike an array, which you can mutate with multiple methods, << being one of them.
add_element explicitly modifies the passed object with <<. If you change the method body to
array = array + [item]
then the output in your second example will be array is [1, 2, 3, 4] as it's a mere reassignment of a local variable.
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%2f53468624%2ffixnum-object-does-not-change-value-after-being-increased%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
There are "special" objects in Ruby that are not mutable. Fixnum is one of them (others are booleans, nil, symbols, other numerics). Ruby is also pass by value.
n = n + 1 does not modify n, it reassigns a local variable in increase's scope.
Since Fixnum isn't mutable, there is no method you could use to change its value, unlike an array, which you can mutate with multiple methods, << being one of them.
add_element explicitly modifies the passed object with <<. If you change the method body to
array = array + [item]
then the output in your second example will be array is [1, 2, 3, 4] as it's a mere reassignment of a local variable.
add a comment |
There are "special" objects in Ruby that are not mutable. Fixnum is one of them (others are booleans, nil, symbols, other numerics). Ruby is also pass by value.
n = n + 1 does not modify n, it reassigns a local variable in increase's scope.
Since Fixnum isn't mutable, there is no method you could use to change its value, unlike an array, which you can mutate with multiple methods, << being one of them.
add_element explicitly modifies the passed object with <<. If you change the method body to
array = array + [item]
then the output in your second example will be array is [1, 2, 3, 4] as it's a mere reassignment of a local variable.
add a comment |
There are "special" objects in Ruby that are not mutable. Fixnum is one of them (others are booleans, nil, symbols, other numerics). Ruby is also pass by value.
n = n + 1 does not modify n, it reassigns a local variable in increase's scope.
Since Fixnum isn't mutable, there is no method you could use to change its value, unlike an array, which you can mutate with multiple methods, << being one of them.
add_element explicitly modifies the passed object with <<. If you change the method body to
array = array + [item]
then the output in your second example will be array is [1, 2, 3, 4] as it's a mere reassignment of a local variable.
There are "special" objects in Ruby that are not mutable. Fixnum is one of them (others are booleans, nil, symbols, other numerics). Ruby is also pass by value.
n = n + 1 does not modify n, it reassigns a local variable in increase's scope.
Since Fixnum isn't mutable, there is no method you could use to change its value, unlike an array, which you can mutate with multiple methods, << being one of them.
add_element explicitly modifies the passed object with <<. If you change the method body to
array = array + [item]
then the output in your second example will be array is [1, 2, 3, 4] as it's a mere reassignment of a local variable.
answered Nov 25 '18 at 14:51
Marcin KołodziejMarcin Kołodziej
4,4901315
4,4901315
add a comment |
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%2f53468624%2ffixnum-object-does-not-change-value-after-being-increased%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
Not all objects are mutable. Numbers, as you found out, are not. If they were, the outcome of
1+2could be anything.– steenslag
Nov 25 '18 at 16:54
See here: stackoverflow.com/questions/28083650/… and warning: constant ::Fixnum is deprecated: stackoverflow.com/a/21411269/5239030
– iGian
Nov 25 '18 at 17:28
What is
p10? .– sawa
Nov 26 '18 at 4:43