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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:16:22+00:00 2026-06-06T08:16:22+00:00

I am currently working on a project where I have code that looks like

  • 0

I am currently working on a project where I have code that looks like this:

  # the first return is the one causing problems
  def collect
    return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]] unless @ident_a_node.nil? and @id.nil?
    return Hash["IdentANode", @id] unless @id.nil?
  end

Where I use the unless operator to conditionally execute the return statement. For some reason this code still executes even if @ident_a_node is nil. When executing I get this message:

IdentANode.rb:14:in collect': undefined methodcollect’ for
nil:NilClass (NoMethodError)

Which confuses me because I had thought that the unless keyword would prevent this from happening. When I change the statement to this form:

if not @ident_a_node.nil? and not @id.nil?
  return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]]
end  

or this form:

return Hash["IdentANode", Array[@id, ",", @ident_a_node.collect]] if not @ident_a_node.nil? and not @id.nil?

The return statement is not executed, what gives? Why is there a difference between these two statements? Does having multiple conditions with the unless keyword cause problems?

Any ideas would be appreciated

  • 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-06T08:16:23+00:00Added an answer on June 6, 2026 at 8:16 am

    You’ve got a logic failure in there. You’re testing that they’re both nil to avoid running it when you should be testing if either is nil. You’ve probably gotten yourself into this situation by having too many layers of negation. Anything more than one is unacceptable.

    In other words, you can get away with “if it’s not raining out” but shouldn’t use things like “unless the is_not_raining flag is not set to the inverse of false”.

    My personal opinion is that trailing conditions should not be used unless it’s obvious that they’re present. As you can see in your example, you have to scroll horizontally to find the condition, hiding important information from the developer.

    As a matter of style, do not use not when ! will do the same job. Secondly, you’re testing specifically against nil when you probably just want a defined value of some kind.

    Other issues include using Hash[] and Array[] which are surely artifacts of using a language which requires them. Ruby, like JavaScript, allows implicit declaration of these using { } and [ ] respectively.

    A proper Ruby-styled version of your code is:

    if (@ident_a_node and @id)
      return { "IdentANode" => [ @id, ",", @ident_a_node.collect ] }
    end  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on code for a Java project, I currently have replaceAll(String, int) in
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
On the project that I'm currently working I have a requirement to run a
This is for an upcoming project. I have two tables - first one keeps
I'm working on a project that is using code from an OpenSource project. One
I am currently working on a project about calculations.I have done the main part
I am currently working on android project where I want to have a text
I am currently working on an android project and I have an activity, lets
I am currently working on grails project. I have created eight different plugins. Each
Im currently working on a grizzly, spring and jersey project and i have encountered:

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.