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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:30:12+00:00 2026-06-05T23:30:12+00:00

In the following program (written in irb shell) i call a method hidden of

  • 0

In the following program (written in irb shell) i call a method hidden of the class Tester that accepts 2 arguments both of them string and then returns a string after modifying them. I get an output which i don’t expect.

What i expect is:

def hidden(aStr,anotherStr) # After the call aStr = suhail and anotherStr = gupta
  anotherStr = aStr + " " + anotherStr
  # After the above statement anotherStr = suhail gupta
  return aStr + anotherStr.reverse 
  # After the above statement value returned should be suhailliahusatpug
  # i.e suhail + "reversed string of suhailgupta"
  end

  # But i get suhailatpug liahus

ACTUAL CODE:

1.9.3p194 :001 > class Tester
1.9.3p194 :002?>   def hidden(aStr,anotherStr)
1.9.3p194 :003?>     anotherStr = aStr + " " + anotherStr
1.9.3p194 :004?>     return aStr + anotherStr.reverse
1.9.3p194 :005?>     end
1.9.3p194 :006?>   end
1.9.3p194 :007 > o = Tester.new
=> #<Tester:0x9458b80> 
1.9.3p194 :008 > str1 = "suhail"
=> "suhail" 
1.9.3p194 :009 > str2 = "gupta"
=> "gupta" 
1.9.3p194 :010 > str3 = o.hidden(str1,str2)
=> "suhailatpug liahus" 

Why is that ? Is it a different matter in Ruby as compared to some other OOP language like Java ?

  • 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-05T23:30:14+00:00Added an answer on June 5, 2026 at 11:30 pm

    The Problem

    You don’t have the string you think you do. Forget the method, just do it line-by-line:

    # You passed these as parameters, so they're assigned.
    aStr = 'suhail'
    anotherStr = 'gupta'
    
    # You re-assign a new string, including a space.
    anotherStr = aStr + " " + anotherStr
    => "suhail gupta"
    
    # You are concatenating two different strings.
    aStr + anotherStr
    => "suhailsuhail gupta"
    
    # You concatenate two strings, but are reversing only the second string.
    aStr + anotherStr.reverse
    => "suhailatpug liahus"
    

    This is all exactly as it should be.

    The Solution

    If you want “suhailatpugliahus” then you need to avoid reassigning a new value to anotherStr.

    def hidden(aStr,anotherStr)
        aStr + (aStr + anotherStr).reverse
    end  
    hidden 'suhail', 'gupta'
    => "suhailatpugliahus"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written down the following program that uses the quicksort algorithm to sort
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have written the following C program. The output is 32. Why is this?
I have the following program to open lot's of sockets, and hold them open
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
The following program will not compile: class Program { static void Main(string[] args) {
I am newbie to PHP I have written the following program: $address=array('abc@gmail.com','abc@hotmail.com','def@yahoo.com'); foreach($address as
I'm just learning java and following a book. I have a program written via
I have a program written in c that gets invoked like: my_program arg1 arg2
I have a program (written in C/C++) that behaves as a socket server. I'd

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.