What do these 2 lines of code actually do?












-5















I don’t know Ruby but I need to understand how the input value is manipulated in the lines below.



I think that the input is converted to the sum of the values of its characters but the second line is confusing; does it take the final value of the sum and perform the bitwise operations or per iteration? Could you provide a simple explanation of the steps followed?



Thank you in advance!



input.to_s.each_char.inject(0) do |sum, ch|
(sum << 8) ^ (ch.ord) ^ (sum >> 4)









share|improve this question


















  • 1





    Is it a checksum? A hash?

    – matt
    Jan 1 at 14:38











  • You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

    – Cary Swoveland
    Jan 1 at 16:15











  • You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

    – Cary Swoveland
    Jan 1 at 16:29













  • @CarySwoveland It does give me the information I needed under, "So in your case:".

    – LostIT
    Jan 2 at 0:15











  • That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

    – Cary Swoveland
    Jan 2 at 1:16
















-5















I don’t know Ruby but I need to understand how the input value is manipulated in the lines below.



I think that the input is converted to the sum of the values of its characters but the second line is confusing; does it take the final value of the sum and perform the bitwise operations or per iteration? Could you provide a simple explanation of the steps followed?



Thank you in advance!



input.to_s.each_char.inject(0) do |sum, ch|
(sum << 8) ^ (ch.ord) ^ (sum >> 4)









share|improve this question


















  • 1





    Is it a checksum? A hash?

    – matt
    Jan 1 at 14:38











  • You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

    – Cary Swoveland
    Jan 1 at 16:15











  • You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

    – Cary Swoveland
    Jan 1 at 16:29













  • @CarySwoveland It does give me the information I needed under, "So in your case:".

    – LostIT
    Jan 2 at 0:15











  • That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

    – Cary Swoveland
    Jan 2 at 1:16














-5












-5








-5


1






I don’t know Ruby but I need to understand how the input value is manipulated in the lines below.



I think that the input is converted to the sum of the values of its characters but the second line is confusing; does it take the final value of the sum and perform the bitwise operations or per iteration? Could you provide a simple explanation of the steps followed?



Thank you in advance!



input.to_s.each_char.inject(0) do |sum, ch|
(sum << 8) ^ (ch.ord) ^ (sum >> 4)









share|improve this question














I don’t know Ruby but I need to understand how the input value is manipulated in the lines below.



I think that the input is converted to the sum of the values of its characters but the second line is confusing; does it take the final value of the sum and perform the bitwise operations or per iteration? Could you provide a simple explanation of the steps followed?



Thank you in advance!



input.to_s.each_char.inject(0) do |sum, ch|
(sum << 8) ^ (ch.ord) ^ (sum >> 4)






ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 13:48









LostITLostIT

8318




8318








  • 1





    Is it a checksum? A hash?

    – matt
    Jan 1 at 14:38











  • You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

    – Cary Swoveland
    Jan 1 at 16:15











  • You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

    – Cary Swoveland
    Jan 1 at 16:29













  • @CarySwoveland It does give me the information I needed under, "So in your case:".

    – LostIT
    Jan 2 at 0:15











  • That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

    – Cary Swoveland
    Jan 2 at 1:16














  • 1





    Is it a checksum? A hash?

    – matt
    Jan 1 at 14:38











  • You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

    – Cary Swoveland
    Jan 1 at 16:15











  • You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

    – Cary Swoveland
    Jan 1 at 16:29













  • @CarySwoveland It does give me the information I needed under, "So in your case:".

    – LostIT
    Jan 2 at 0:15











  • That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

    – Cary Swoveland
    Jan 2 at 1:16








1




1





Is it a checksum? A hash?

– matt
Jan 1 at 14:38





Is it a checksum? A hash?

– matt
Jan 1 at 14:38













You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

– Cary Swoveland
Jan 1 at 16:15





You need to tell us what input is. Not just its class (Integer?), but more generally what it represents. We also need to understand how the return value of your code is used (related to @matt's question, which you need to answer). Lastly, you need a third line of code containing just end. Please address these issues by editing your question rather than by elaborating in comments.

– Cary Swoveland
Jan 1 at 16:15













You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

– Cary Swoveland
Jan 1 at 16:29







You have selected an answer that does nothing more than explain how inject works, with no reference to the particular calculation you wish to perform. There are thousands of explanations of inject/reduce within easy reach, many with greater detail than the explanation contained in the selected answer,. That show of laziness earns a downvote from me.

– Cary Swoveland
Jan 1 at 16:29















@CarySwoveland It does give me the information I needed under, "So in your case:".

– LostIT
Jan 2 at 0:15





@CarySwoveland It does give me the information I needed under, "So in your case:".

– LostIT
Jan 2 at 0:15













That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

– Cary Swoveland
Jan 2 at 1:16





That may be, but it's not at all clear from the question what you want. Judging from the voting I don't seem to be alone in my assessment of the quality of the question.

– Cary Swoveland
Jan 2 at 1:16












1 Answer
1






active

oldest

votes


















2














inject is the same as reduce, and is similar to reduce in many other languages.



There are a number of different ways to call it but the way shown in the question is this:



inject(initial) { |memo, obj| block } → obj



If you specify a block, then for each element in enum the block is
passed an accumulator value (memo) and the element. If you specify a
symbol instead, then each element in the collection will be passed to
the named method of memo. In either case, the result becomes the new
value for memo. At the end of the iteration, the final value of memo
is the return value for the method.



If you do not explicitly specify an initial value for memo, then the
first element of collection is used as the initial value of memo.




So in your case:



input.to_s.each_char.inject(0) do |sum, ch|
(sum << 8) ^ (ch.ord) ^ (sum >> 4)
end


The initial value of sum is 0, which is used for the first iteration, but for each subsequent iteration the result of the block is used for sum in the next iteration.



For example the following should produce the same value without using inject/reduce.



sum = 0
input.to_s.each_char do |ch|
sum = (sum << 8) ^ (ch.ord) ^ (sum >> 4)
end





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%2f53995982%2fwhat-do-these-2-lines-of-code-actually-do%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









    2














    inject is the same as reduce, and is similar to reduce in many other languages.



    There are a number of different ways to call it but the way shown in the question is this:



    inject(initial) { |memo, obj| block } → obj



    If you specify a block, then for each element in enum the block is
    passed an accumulator value (memo) and the element. If you specify a
    symbol instead, then each element in the collection will be passed to
    the named method of memo. In either case, the result becomes the new
    value for memo. At the end of the iteration, the final value of memo
    is the return value for the method.



    If you do not explicitly specify an initial value for memo, then the
    first element of collection is used as the initial value of memo.




    So in your case:



    input.to_s.each_char.inject(0) do |sum, ch|
    (sum << 8) ^ (ch.ord) ^ (sum >> 4)
    end


    The initial value of sum is 0, which is used for the first iteration, but for each subsequent iteration the result of the block is used for sum in the next iteration.



    For example the following should produce the same value without using inject/reduce.



    sum = 0
    input.to_s.each_char do |ch|
    sum = (sum << 8) ^ (ch.ord) ^ (sum >> 4)
    end





    share|improve this answer




























      2














      inject is the same as reduce, and is similar to reduce in many other languages.



      There are a number of different ways to call it but the way shown in the question is this:



      inject(initial) { |memo, obj| block } → obj



      If you specify a block, then for each element in enum the block is
      passed an accumulator value (memo) and the element. If you specify a
      symbol instead, then each element in the collection will be passed to
      the named method of memo. In either case, the result becomes the new
      value for memo. At the end of the iteration, the final value of memo
      is the return value for the method.



      If you do not explicitly specify an initial value for memo, then the
      first element of collection is used as the initial value of memo.




      So in your case:



      input.to_s.each_char.inject(0) do |sum, ch|
      (sum << 8) ^ (ch.ord) ^ (sum >> 4)
      end


      The initial value of sum is 0, which is used for the first iteration, but for each subsequent iteration the result of the block is used for sum in the next iteration.



      For example the following should produce the same value without using inject/reduce.



      sum = 0
      input.to_s.each_char do |ch|
      sum = (sum << 8) ^ (ch.ord) ^ (sum >> 4)
      end





      share|improve this answer


























        2












        2








        2







        inject is the same as reduce, and is similar to reduce in many other languages.



        There are a number of different ways to call it but the way shown in the question is this:



        inject(initial) { |memo, obj| block } → obj



        If you specify a block, then for each element in enum the block is
        passed an accumulator value (memo) and the element. If you specify a
        symbol instead, then each element in the collection will be passed to
        the named method of memo. In either case, the result becomes the new
        value for memo. At the end of the iteration, the final value of memo
        is the return value for the method.



        If you do not explicitly specify an initial value for memo, then the
        first element of collection is used as the initial value of memo.




        So in your case:



        input.to_s.each_char.inject(0) do |sum, ch|
        (sum << 8) ^ (ch.ord) ^ (sum >> 4)
        end


        The initial value of sum is 0, which is used for the first iteration, but for each subsequent iteration the result of the block is used for sum in the next iteration.



        For example the following should produce the same value without using inject/reduce.



        sum = 0
        input.to_s.each_char do |ch|
        sum = (sum << 8) ^ (ch.ord) ^ (sum >> 4)
        end





        share|improve this answer













        inject is the same as reduce, and is similar to reduce in many other languages.



        There are a number of different ways to call it but the way shown in the question is this:



        inject(initial) { |memo, obj| block } → obj



        If you specify a block, then for each element in enum the block is
        passed an accumulator value (memo) and the element. If you specify a
        symbol instead, then each element in the collection will be passed to
        the named method of memo. In either case, the result becomes the new
        value for memo. At the end of the iteration, the final value of memo
        is the return value for the method.



        If you do not explicitly specify an initial value for memo, then the
        first element of collection is used as the initial value of memo.




        So in your case:



        input.to_s.each_char.inject(0) do |sum, ch|
        (sum << 8) ^ (ch.ord) ^ (sum >> 4)
        end


        The initial value of sum is 0, which is used for the first iteration, but for each subsequent iteration the result of the block is used for sum in the next iteration.



        For example the following should produce the same value without using inject/reduce.



        sum = 0
        input.to_s.each_char do |ch|
        sum = (sum << 8) ^ (ch.ord) ^ (sum >> 4)
        end






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 14:38









        nPnnPn

        8,27362245




        8,27362245
































            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%2f53995982%2fwhat-do-these-2-lines-of-code-actually-do%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas