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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:22:23+00:00 2026-06-03T16:22:23+00:00

I wrote the following Ruby code: def myItems if @item1 yield @item1 end if

  • 0

I wrote the following Ruby code:

def myItems
    if @item1
        yield @item1
    end
    if @item2
        yield @item2
    end
end

Now I tried to use:

myItems.each do |item|
    puts item
end

However, when both @item1 and @item2 are nil, I get the error:

Error: #<NoMethodError: undefined method `each' for nil:NilClass>.

I would expect an equivalent to “yield break” in C# to prevent this. Does anyone know how this works in Ruby?

  • 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-03T16:22:25+00:00Added an answer on June 3, 2026 at 4:22 pm

    It’s hard to tell what you are trying to do, but I think you want simply:

    myItems do |item|
      puts item
    end
    

    Your current code is assuming that the return value of myItems is an enumerable (e.g. an array), and under no circumstance (even if your @items are not nil) is that the return value.

    Alternatively, do either this:

    # Method that iterates over all items
    def eachItem
      [@item1,@item2].compact.each do |item|
        yield item
      end
    end
    
    eachItem do |item|
      puts item
    end
    

    …or this:

    # Give me an array of all non-nil items
    def myItems
      [@item1,@item2].compact
    end
    
    myItems.each do |item|
      puts item
    end
    

    Note: I’ve used camelCase method names because that’s what you have originally, but note that it’s idiomatic in Ruby to use snake_case instead.

    Note 2: storing multiple similar items as instance variables instead of as a collection seems less useful than storing the collection itself. However, without knowing your data I cannot suggest a better way to represent this.

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

Sidebar

Related Questions

I have the following Ruby code: module MyModule class MyClass def self.my_method end end
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
if wrote the following code and do not understand why the trace returns false:
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following regex in perl and now i want to write it
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32
I'm studying Ruby and my brain just froze. In the following code, how would
I am using Ruby 1.8.6 for the following code: # Create an array and
The following code Ruby code will iterate the source string and produce a list

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.