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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:02:38+00:00 2026-05-24T20:02:38+00:00

I need to check if a variable is an array, and if not convert

  • 0

I need to check if a variable is an array, and if not convert it into one before proceed with further processing. So, my code looks like this:

class Test < Struct.new(:args)
    def eval
        p "1. #{args}"
        args = (args.instance_of? Array) ? args : [args]
        p "2. #{args}" # woah! [nil]?
        # ...other things, with "args" being an array for sure..or not?!?
    end
end

I am quite new to ruby, so perhaps this is not very idiomatic, but to me looks like this code should at least work. Instead, the second time I print the args variable, it is [nil].
Notice that if I change the method eval slightly:

def eval
    p "1. #{args}"
    a = args
    args = (a.instance_of? Array) ? a : [a]
    p "2. #{args}"
end

everything works as expected. So, is there something very specific to the Struct class that I don’t get it, or something fishy is going on here?
(using ruby 1.9.3-dev on macosx, using rvm)

  • 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-24T20:02:39+00:00Added an answer on May 24, 2026 at 8:02 pm

    Actually there’s a Ruby idiom for what you are trying to do: [*args]. * in this context is called the splat operator:

    http://raflabs.com/blogs/silence-is-foo/2010/08/07/ruby-idioms-what-is-the-splatunary-operator-useful-for/

    If you get passed an array, splat will “flatten” the array into the new one, if you pass a single argument, it will become a one element array.

    For the odd behavior: it looks to me like you create a local variable args in your eval method, which gets initialized to nil because it’s on the LHS of an assignment. Then the ternary evaluates to false because args is not an array and makes an array of the current value, which is still nil. If args would be an instance variable (@args), things would work the way you expect. In other words, while inheriting from the Struct will give you args and args= methods, it won’t give you an @args instance variable.

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

Sidebar

Related Questions

I have an array variable like Array[0] , and now I need to check
Is this a possible function? I need to check if a variable is existent
I need to check to see if a variable contains anything OTHER than a-z
For a check i need yesterday's date at 10:00pm in a variable. I get
i want something like this the user enter a website link i need check
I need to check whether a page is being redirected or not without actually
I need to check all of my asp code to prevent SQL injection. Should
if you have a array like ('a', 'b') and check $.inArray('a', thearray); you get
Is there any way to efficiently check if the variable is Object or Array,
I need to check programmatically (in .NET) whether a given user (domain account) is

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.