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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:56:46+00:00 2026-05-23T03:56:46+00:00

My Ruby program reads lines from stdin and uses puts to print to stdout

  • 0

My Ruby program reads lines from stdin and uses puts to print to stdout (the terminal). Can I use RSpec to test the reads and writes? Can I inject a string to my program like it was written in stdin and at the same time check the output?

line = STDIN.read.chomp.split

Also, I have the reads and writes in a loop, until line[0] is “quit”. Can I test while the loop is running or should I call subject.read_in and subject.write_out?

  • 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-23T03:56:46+00:00Added an answer on May 23, 2026 at 3:56 am

    You can use mocks and have the method called more than once by listing multiple values in the and_return() method. These will be returned, one on each call, in the order given.

    STDIN.should_receive(:read).and_return("Your string")
    
    STDIN.should_receive(:read).and_return("value1", "value2", "value3")
    

    You can do similar things with STDOUT:

    STDOUT.should_receive(:puts).with("string")
    

    See the RSpec mocking documentation for more information.

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

Sidebar

Related Questions

I am writing a test program with Ruby and ActiveRecord, and it reads a
I can run Bash shell commands from with a Ruby program or irb using
How do I call shell commands from inside of a Ruby program? How do
I'd like to be able to write a ruby program that can restart without
So all I simply want to do is make a Ruby program that reads
I'm writing a program in Ruby that downloads a file from an RSS feed
When would you use this garbage collection method in your Ruby program(s)? GC.start
I want to create an object from a Ruby program and store it in
I'm writing a ruby program that executes some external command-line utilities. How could I
$0 is the variable for the top level Ruby program, but is there one

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.