Fixnum object does not change value after being increased












0















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










share|improve this question























  • 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











  • What is p10? .

    – sawa
    Nov 26 '18 at 4:43
















0















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










share|improve this question























  • 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











  • What is p10? .

    – sawa
    Nov 26 '18 at 4:43














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • What is p10? .

    – 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











  • 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

















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












1 Answer
1






active

oldest

votes


















4














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    4














    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.






    share|improve this answer




























      4














      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.






      share|improve this answer


























        4












        4








        4







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 14:51









        Marcin KołodziejMarcin Kołodziej

        4,4901315




        4,4901315
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Futebolista

            Feedback on college project

            Albești (Vaslui)