1

Sorry for my probably noobish question; I recently started developing with Rails. I checked through the API, documentation, and did a bunch of searches but could not find what I was look for.

Is there a method to check to see if a specific array exists?

For example,

    array = []
    array = [2,3,4]
    if array.exists?
      puts "array exists!"
    else
      puts "No such thing!"
    end

Thanks

2
  • What do you mean by exists? Has content? Commented Jun 4, 2015 at 14:19
  • No, More like is declared. Commented Jun 4, 2015 at 14:24

4 Answers 4

5

like so:

if defined?(array)

instance variables (eg @array) default to nil, so you can just test them with

if @array
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your lightning fast answer!
2

Note: By defining your array with array = [], your array will always exist. But if you want to check if array is an Array, you can say array.is_a?(Array)

Comments

0

Try this

a = [1,2,3]

a.any?
=> true

a.clear

a.any?
=> false

Comments

0

Hi fellas i´m newbie at Ruby on Rails but this is the most simple way:

if (defined?(array))
   puts "array exists!"
else
   puts "No such thing!"
end

1 Comment

Thanks for the precise example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.