Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8796885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:43:29+00:00 2026-06-13T23:43:29+00:00

I understand what is happening in this skip_tracks method, however, I never could have

  • 0

I understand what is happening in this skip_tracks method, however, I never could have written it this way, because I don’t understand the rule underlying the behavior in the block. This is the part that’s puzzling me

 skip.times { playlist.push playlist.shift}

Is there a rule that it executes right to left, so that the value that’s shifted is returned and pushed onto playlist? How does one know the value that’s shifted is going to be pushed onto playlist? What’s happening inside the block to make this a rule? Can you explain…Also, if this block had been written over multiple lines, playlist.push would have been above playlist.shift, and therefore executed first, but this code requires that the value be shifted before pushed


def skip_tracks(playlist, skip)
  if skip > 0
    skip.times { playlist.push playlist.shift}
  else
    skip.abs.times { playlist.unshift playlist.pop }
  end
  puts playlist
end


playlist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
skip_tracks(playlist, 2)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:43:30+00:00Added an answer on June 13, 2026 at 11:43 pm

    Think of push and unshift as functions that take an argument and put the element passed to it into the list; and shift and pop as functions that remove and return an element.

    If I write what’s happening using c-like function syntax, you get:

    playlist.push(playlist.shift())
    

    and

    playlist.unshift(playlist.pop())
    

    Therefore it will resolve these function calls from the inside out and you get the behavior you see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't understand why this is happening: I have an integer being passed to
I don't really understand why this is happening at all. I'm trying to open
I don't understand what's happening here. If someone could explain, I would appreciate it.
I don't understand why this is happening. I need to get the objects startPoint
I really do not understand how is this error happening at this code. Please
I am trying to understand what is happening in the getter below, this is
I don't understand what happening in my application. My app structure: EventListActivity (List of
I don't quite understand whats happening here a=[1,2,3,4] b=[5,6,7,8] a[len(a):] = b seems to
I just don't understand what's happening, I haven't change anything to the site for
I understand this is an old technology, but I have been using it forever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.