Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Ruby Undefined Method each for NilClass Error

charlotte

Coder
Hi everyone,

I'm running into an issue with my Ruby code and I'm not sure how to fix it. I keep getting the following error:
Code:
undefined method `each` for nil:NilClass (NoMethodError)

Here is a simplified version of my code:

Code:
def print_items(items)
  items.each do |item|
    puts item
  end
end

my_items = nil
print_items(my_items)
I also check this : https://codeforum.org/tags/ergenairor/ But I have not found any error.

I understand that this error occurs because my_items is nil, but I'm not sure how to properly handle this situation. Should I be checking if items is nil before calling each, or is there a better way to handle this?

Any advice or suggestions on how to fix this error would be greatly appreciated!

Thanks in advance!
 
Back
Top Bottom