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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:40:36+00:00 2026-06-10T07:40:36+00:00

The code below works as I want, switching a light on or off with

  • 0

The code below works as I want, switching a light on or off with each keypress of l. However, when I try to add other things I want done with the switching, I can’t.

look = 0
lighton = 0
while look < 10
  system 'stty cbreak'
  q = $stdin.sysread 1
  case  q ### #{q} 
  when "l" then if lighton==1 then lighton=0  and puts "ight off"
                else lighton=1 and puts "ight on" end
  end
  system 'stty cooked'
  look += 1
end #while

If I add another and it isn’t seen but I get no error:

look = 0
lighton = 0
while look <10
  system 'stty cbreak'
  q = $stdin.sysread 1
  case  q ### #{q} 
  when "l" then if lighton==1 then lighton=0  and puts "ight off" and puts "light still off"
                else lighton=1 and puts "ight on" end
  end
  system 'stty cooked'
  look += 1
end #while

I’d like to add several more statements to both the if and else portions but can’t.

  • 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-10T07:40:37+00:00Added an answer on June 10, 2026 at 7:40 am

    There’s no limit, you’re just misusing the and operator. It’s not meant to do “this and that”, it does “do this and, if it is truthy, do this too”. Here’s a quick example:

    1 and puts 'falsy'
    nil and puts 'truthy'
    # prints: falsy
    

    Since puts returns nil, which is falsy, puts 'hello' and puts 'world' will only print “hello”.

    So, don’t use and to create a one-liner. You could use ; instead, but that doesn’t help readability. Instead just use multiple lines! It’s much clearer what’s going on:

    case  q
    when "l"
      if lighton == 1
        lighton = 0
        puts "light off"
        puts "light still off"
      else
        lighton = 1
        puts "light on"
      end
    end
    

    You may wish to read more about and/or in Ruby and how they differ from &&/||.

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

Sidebar

Related Questions

The code below works fine at 1280x800, however, if I add another thumbnail, the
I have the following code below which works, but I want to insert values
See code below: I want Page 1 to alert #testing... This works in C
I'm just looking to optimize the code below. It works, but I want to
i want to put the id into the colorbox below this code nom works
The code below works well right now; however, it's serially executed. I'd like to
The code below works but I want to optimize the code using yield or
The XAML code below works fine except I want the expander button to be
I want to change JDialog icon. Code below works for *.jpg files, but doesn't
The code below works exactly as I want it to in my perl script.

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.