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

  • Home
  • SEARCH
  • 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 974523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:27:01+00:00 2026-05-16T03:27:01+00:00

I have some methods for a class which return arrays like [1, 3, 2,

  • 0

I have some methods for a class which return arrays like ["1", "3", "2", "6", "2"]. It is ok that these are string arrays, not numeric. I have another method that takes an array, and turns it into a single string like this 1 3 2 6 2.

class Turn
  def initialize
    @rolls = 1
    @dice = []
  end

  def roll
    @roll = []
    x = 5 - @dice.length
    x.times do |i|
      @roll[i] = rand(6) + 1
    end
    @roll  # ["1", "3", "2", "6", "2"]
  end

  def show
    @dice  # ["1", "3", "6"]
  end

  def line(something)
    temp = ""
    something.each do |x|
      temp << x + " "
    end
    puts temp   # "1 3 6 " or "1 3 2 6 2 "
  end
end

Then I want to be able to chain methods together so I could do

first = Turn.new
first.roll.line
first.show.line

However, I really don’t know how to do this, and I haven’t been able to find how online. I have seen self get returned, but I can’t really figure out what it does. The code in the line method works if I move it into another method, so the problem is really just dealing with the chaining and returning.

If someone could not only help with code, but with how chaining works with regards to return values and how returning self works, that would be awesome.

Thank you!

  • 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-05-16T03:27:01+00:00Added an answer on May 16, 2026 at 3:27 am

    Since your #roll and #show methods return an array, you’d need to define a method Array#line to actually be able to do first.roll.line.

    Btw, there’s already Array#join which also joins array elements together to a string, just like your #line method, so you could as well use first.roll.join(' ') and get rid of your #line method.

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

Sidebar

Related Questions

I have a simple Java class that has some methods: public class Utils {
I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden
I have: @Component class MyDecorator{ private Cache cache; /* some wrapped methods like get
I have a State class that includes configuration as well as some state-specific methods.
I have a class with some method that depend by one parameter. What is
I have some class that I'm passing as a result of a service method,
I am looking to do some method chaining. I have the following code: class
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
I have an Entity that looks like this: class Privilege { /** * @Id
I have this: one string variable which holds the class name ( $classname )

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.