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 8643607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:05:29+00:00 2026-06-12T12:05:29+00:00

Im currently going through a book and there is a pice of code that

  • 0

Im currently going through a book and there is a pice of code that I don’t quite understand:

class RevealingReferences
  attr_reader :wheels

  def initialize(data)
    @wheels = wheelify(data)
    puts data
  end

  def diameters
    wheels.collect do |wheel|
      puts "test"
      wheel.rim + (wheel.tire*2)
    end
  end

  Wheel = Struct.new(:rim, :tire)

  def wheelify(data)
    data.collect{|cell|
      Wheel.new(cell[0], cell[1])}
    end
  end
end

puts RevealingReferences.new([3,2,5,8]).diameters

and I get the following output:

3
2
5
8
test
test
test
test
3
2
1
0

1) Now the 3,2,5,8 I understand, but why does not display in array format [3,2,5,8] rather its being displayed one int at a time.

2) Also, in the wheels.collect block, the output prints “test” twice before putting in the output, should it not be “test” value “test” value

3) Also, the answer 3,2,1,0 don’t make any sense, when I set @wheels should wheels not be a collection of an array of 2 elements rather then 4?

  • 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-12T12:05:31+00:00Added an answer on June 12, 2026 at 12:05 pm

    1) Now the 3,2,5,8 I understand, but why does not display in array
    format [3,2,5,8] rather its being displayed one int at a time.

    This is due to how puts works. When it sees an array, it prints the #to_s of each element

    puts [1,2,3]
    # >> 1
    # >> 2
    # >> 3
    

    If you want it to look like an array, you should inspect it before printing it

    puts [1,2,3].inspect
    # >> [1, 2, 3]
    

    There’s also a shorthand for this, the method p

    p [1,2,3]
    # >> [1, 2, 3]
    

    2) Also, in the wheels.collect block, the output prints “test” twice
    before putting in the output, should it not be “test” value “test”
    value

    The only thing printing the values is the puts statement on the return value of diameters, so they won’t print until after they have been collected. If you wanted to print it after each test, you should probably do something like

    def diameters
      wheels.collect do |wheel|
        puts "test"
        p wheel.rim + (wheel.tire*2)
      end
    end
    

    Which would print:

    test
    3
    test
    2
    test
    1
    test
    0
    

    3) Also, the answer 3,2,1,0 don’t make any sense, when I set @wheels
    should wheels not be a collection of an array of 2 elements rather
    then 4?

    Based on what you’re saying here, I assume your data is not in the format you intended. You’re passing in [3,2,5,8], but this implies that you meant to pass in [[3,2],[5,8]], or to map across every pair of values:

    def wheelify(data)
      data.each_slice(2).collect do |cell|
        Wheel.new(cell[0], cell[1])
      end
    end
    

    The reason it isn’t doing what you think is because without doing one of these, the cell variable is actually just a number. Since numbers have the brackets method defined on them, they wind up working in this case. But the brackets method just returns 1 or 0, depending on the bit (base 2) at that position:

    five = 5
    five.to_s(2) # => "101"
    five[2] # => 1
    five[1] # => 0
    five[0] # => 1
    

    So in the case of 3, wheel.rim + (wheel.tire*2) becomes

    cell = 3
    cell.to_s(2)   # => "11"
    rim = cell[0]  # => 1
    tire = cell[1] # => 1
    rim + tire * 2 # => 3
    

    And in the case of 2:

    cell = 2
    cell.to_s(2)   # => "10"
    rim = cell[0]  # => 0
    tire = cell[1] # => 1
    rim + tire * 2 # => 2
    

    And 5:

    cell = 5
    cell.to_s(2)   # => "101"
    rim = cell[0]  # => 1
    tire = cell[1] # => 0
    rim + tire * 2 # => 1
    

    And 8:

    cell = 8
    cell.to_s(2)   # => "1000"
    rim = cell[0]  # => 0
    tire = cell[1] # => 0
    rim + tire * 2 # => 0
    

    Which is why diameters returns [3, 2, 1, 0], explaining the last four digits you see.

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

Sidebar

Related Questions

Pointer related question. I'm going through some example code that currently reads in data
I'm currently going through the process of running FxCop through our application that has
I'm going to assume that the overall structure of my code as it currently
I'm currently developping an app that is going through all the files on a
I'm currently going through Stoyan Stefanov's book Object-oriented JavaScript and I've stumbled on an
I am currently going through the tutorial examples on http://code.google.com/p/stanford-cs193g-sp2010/ to learn CUDA. The
I'm currently going through the book Learning Python The Hard Way, and I'm trying
I am currently going through the book Head First Object Oriented Analysis and Design
I am going through Zed Shaw's Python Book. I am currently working on the
I'm going through the Django book and I'm currently on chapter 10 . I'm

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.