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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:52:47+00:00 2026-06-06T02:52:47+00:00

I want do puts blob but if the blob variable doesn’t exist, I get

  • 0

I want do puts blob

but if the blob variable doesn’t exist, I get

NameError: undefined local variable or method `blob' for main:Object

I’ve tried

blob?  
blob.is_a?('String')
puts "g" if blob  
puts "g" catch NameError
puts "g" catch 'NameError'

but none work.

I can get around it by using an @instance variable but that feels like cheating as I should know about and deal with the issue of no value accordingly.

  • 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-06T02:52:48+00:00Added an answer on June 6, 2026 at 2:52 am

    In this case, you should do:

    puts blob if defined?(blob)
    

    Or, if you want to check for nil too:

    puts blob if defined?(blob) && blob
    

    The defined? method returns a string representing the type of the argument if it is defined, or nil otherwise. For example:

    defined?(a)
    => nil
    a = "some text"
    => "some text"
    defined?(a)
    => "local-variable"
    

    The typical way of using it is with conditional expressions:

    puts "something" if defined?(some_var)
    

    More about defined? on this question.

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

Sidebar

Related Questions

When I do puts /<title>(.*?)<\/title>/.match(html) I get <h2>foobar</h2> But I want just foobar What's
I want to create a class Nilly to obtain a pseudo-nil object, but I
When the user puts his mouseover this object i want a message to pop
I want to make one thread that puts values to a queue when it
I have a junk folder, where my application puts temporary files. I don't want
Want the function to sort the table by HP but if duplicate HPs then
I want it so that if someone visits: http://www.site.com/#hash It puts the data after
class Foo def self.bar puts foobar end def bar self.class.bar end end I want
I want 'This Is A 101 Test' to be 'This Is A Test', but
I want to test a method defined in a rake task. rake file #lib/tasks/simple_task.rake

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.