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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:06:29+00:00 2026-06-04T15:06:29+00:00

So I am pushing some elements on my array like this: upd_city_list << [

  • 0

So I am pushing some elements on my array like this:

upd_city_list << [ j.children[0].text.strip!.gsub(/\s+\W/, ''), j.children[1].text, j.children[1][:href] ]

The above is in an iterator (hence the use of j).

The issue is that from time to time, the j.children[0].text turns up as nil, and Ruby doesn’t like that.

I could add a bunch of if statements before this assignment, but that seems a bit inelegant to me.

How do I handle nil cases in this situation in an elegant way?

One possible solution is, when there is a nil value, just push the string none onto the array….but what would that look like?

Thanks.

Edit1:

This is the error I am getting:

NoMethodError: private method ‘gsub’ called for nil:NilClass
  • 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-04T15:06:30+00:00Added an answer on June 4, 2026 at 3:06 pm

    The real problem is that strip! returns nil when there are no changes to the string. Your text method is returning a string, it is your strip! method is returning nil. I don’t know why it does this. I dislike it, too.

    This case of the problem will go away if you just change strip! to strip

    In a more general sense, you might create an object to return the array for you. You don’t want to go changing (what I assume is) Nokogiri, but you can wrap it in something to hide the train wrecks that result.

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

Sidebar

Related Questions

How could you run some action when pushing a toggle button like this: {
I am pushing some text to an iPhone app. once the message is accepted
This might be pushing the limits here. I iframe some external websites into one
I would like to build a website that has some elements of a social
Looking for some opinions on this and trying to start pushing forward with a
I have some JSON that looks like this: { ST: Security, C1: Login failures,
I created a stack class like this. Some times it runs and sometimes it
This has boggled me for some time, but I managed to keep pushing it
I need some advice on what kind of pattern(s) I should use for pushing/pulling
In my application, I need to do some activity i.e pushing otherview controller,when I

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.