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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:40:07+00:00 2026-05-28T05:40:07+00:00

In ruby, I’m not quite sure how to handle whether objects are nil or

  • 0

In ruby, I’m not quite sure how to handle whether objects are nil or not.

For example I have the following:

begin
    sp = SerialPort.new(@serial_device, @serial_bps, @serial_par, @serial_bits, SerialPort::NONE)
    tcp = TCPSocket.new(@host, @port)

    if (sp)
        sp.print(command)
        sp.close
    elsif
        tcp.print(command)
        tcp.close
    end

    say siri_output
rescue
    pp $!
    puts "Sorry, I encountered an error: #{$!}"
ensure
    request_completed 
end

The problem is that the first object returns an error relating to:

No route to host - connect(2)

Which is correct, because TCP isn’t connected duh. So I’d like it to use the next object instead.

Is there a way to do this without using certain Exceptions, I was wondering if there’s a better way of doing what I’m after any how.

  • 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-28T05:40:07+00:00Added an answer on May 28, 2026 at 5:40 am

    The problem is not the checks, you are doing that right. Anything that’s not nil or false is true in ruby. It’s that when you get an exception on the row that starts with “sp = ..” the execution jumps to the resque block. You should restructure the code like this (I’ve removed the ensure clause because I do not know what it does). A good thing to do it’s to rescue every specific type of exception in it’s own row. by class name ex. NoConnectivityException => e (or what the class of the exception would be).

     begin
         sp = SerialPort.new(@serial_device, @serial_bps, @serial_par, @serial_bits, SerialPort::NONE)
         sp.print(command)
         sp.close
         say siri_output
     rescue Exception => e
         puts "Sorry, I encountered an error: #{e.inspect}"
         puts "trying TCP"
         begin
             tcp = TCPSocket.new(@host, @port)
             tcp.print(command)
             tcp.close
             say siri_output
         rescue Exception => e
             puts "Sorry, I encountered an error: #{e.inspect}"
         end
     end
    

    For quick and sloppy programming you can do another thing, but it’s not recommended and generally a pain to debug, as any error results in nil and is silenced.

    sp = SerialPort.new(@serial_device, @serial_bps, @serial_par, @serial_bits, SerialPort::NONE) rescue nil
    tcp = TCPSocket.new(@host, @port) rescue nil
    

    This way you’d end up with either a SerialPort object or nil object in the sp variable, and the same for sp.

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

Sidebar

Related Questions

Ruby has two different exceptions mechanisms: Throw/Catch and Raise/Rescue. Why do we have two?
Ruby setters—whether created by (c)attr_accessor or manually—seem to be the only methods that need
Ruby's standard popen3 module does not work on Windows. Is there a maintained replacement
Ruby on Rails does not do multithreaded request-responses very well, or at least, ActiveRecord
Ruby on Rails controllers will automatically convert parameters to an array if they have
ruby-1.8.7-p249 > xml = Builder::XmlMarkup.new => <inspect/> ruby-1.8.7-p249 > xml.foo '<b>wow</b>' => <inspect/><foo>&lt;b&gt;wow&lt;/b&gt;</foo> ruby-1.8.7-p249
Ruby has conditional initialization. Apparently, Java does not or does it? I try to
Ruby has Enumerable#max and Enumerable#max_by and I am using max_by in the following way.
Ruby is preinstalled on my Mac and so I wanted to have a look
Ruby example: name = Spongebob Squarepants puts Who lives in a Pineapple under the

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.