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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:54:15+00:00 2026-06-06T06:54:15+00:00

I have one single file called Exercises.rb def ask(prompt) print prompt, ‘ ‘ $stdout.flush

  • 0

I have one single file called Exercises.rb

def ask(prompt)
  print prompt, ' '
  $stdout.flush
  s = gets
  return s
end

def myreverse(s)
  aux=""
  for i in 0..s.length-1
    aux=s[i] + aux
  end
  return aux
end

def mywordreverse(s)
  aux=[]
  s=s.split(" ")
  for i in 0..s.length-1
    aux.unshift(s[i])
  end
  return aux.join(" ")
end

def choose(s,option)
  case option
    when 1 then print myreverse(s)
    when 2 then print mywordreverse(s)
    when 3 then print "hello"
    else print "You gave me #{option} -- I have no idea what to do with that."
  end
end

s=ask("Write a string to reverse: ")
option=ask("Choose an option. 1 - Reverse string. 2 - Reverse String words : ")

choose(s,option)

I am always getting You gave MYCHOSENOPTION -- I have no idea what to do with that., no matter what option I choose. If I put an if just before the case comparing 1, it just doesn’t seem to be matching the option to my strings.

  • 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-06T06:54:18+00:00Added an answer on June 6, 2026 at 6:54 am

    FWIW, here is how I would write this program:

    def ask(prompt)
      print "#{prompt} "
      gets.chomp
    end
    
    def myreverse(s)
      s.reverse
    end
    
    def mywordreverse(s)
      s.split(' ').reverse.join(' ')
    end
    
    def choose(s,option)
      case option
        when 1 then puts myreverse(s)
        when 2 then puts mywordreverse(s)
        when 3 then puts "hello"
        else        puts "You gave me #{option}; I don't know what to do with that."
      end
    end
    
    $stdout.sync
    str    = ask("Write a string to reverse: ")
    option = ask("Choose an option:\n1: Reverse string\n2: Reverse String words\n>")
    choose(str,option.to_i)
    

    Notes:

    1. The last expression in a method is the return value; using return is almost never needed or desirable in Ruby.
    2. There exist built-in methods for reversing strings and arrays. (I understand if you are doing this for an exercise.)
    3. It is cumbersome to iterate arrays or strings in Ruby using for. Instead, you should use

      my_str.each_char do |char|
        # use the single-character string `char` here
      end
      
      my_array.each do |item|
        # use the item here
      end
      
    4. You can use $stdout.sync to force output to always be flushed.

    5. You need to use chomp on your string to remove the trailing newline always included when the user presses Enter.
    6. As pointed out by @robbrit, the core of your problem is that the return value of gets is a String, and you are comparing it to a Fixnum. I’ve used to_i in my code above to convert the string to an integer for comparison.
    7. I’ve used puts instead of print for the output so that I get a newline at the end and do not leave the user with their next command prompt on the same line as the output.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have merged multiple jpeg files into one single .bin file. ..... ......... while(true){
I have two different modules that need access to a single file (One will
In all the examples (leaderboard, wordplay, etc.) they have one single HTML template file.
i have one single class for margin and i am using this class to
** I want one single list item to have fistname, last name and the
I have created a binded DataGridView that has one single column. I want to
I have one master report with sub report. in master report display single value
Any one have an idea to share running multiple tomcats on single machine with
How to merge the static libraries into single one? I do have three static
In one of my first Cocoa applications I have just a single window with

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.