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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:00:43+00:00 2026-05-29T10:00:43+00:00

To learn Ruby, I’m implementing different data structures starting with nodes and a simple

  • 0

To learn Ruby, I’m implementing different data structures starting with nodes and a simple stack. If I matching each def with a corresponding end, there are lots of error about expecting $end (EOF) but getting end. So I could fix it by stacking some ends at the end of the class, but obviously I don’t know why that works.

require "Node"
class Stack
    attr_accessor :top

    def size
            @size
    end

    def push(node)
        if node && node.next
            node.next = top
            top = node
        end
        size++
    def pop()
        if top != nil
            top = top.next
        end
        size--


    def to_s
        if top != nil
            temp = top
            while temp != nil
                puts temp.value
                temp = temp.next
            end
        else
            puts "The stack is empty"
        end
    end
end
end
end

The node class is very simple and shouldn’t cause any problems:

class Node
    attr_accessor :next
    def initialize(value)
        @value = value
    end
end

Everything works fine on that Frankenstein Stack, except pushing a node results in NoMethodError: undefined method +@' for nil:NilClass. Not sure if that is related, but I’m mostly concerned with the syntax of method/class declaration and using end

  • 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-29T10:00:44+00:00Added an answer on May 29, 2026 at 10:00 am

    You get an error because ruby does not have ++ and -- operators.

    Ruby understand the following constructs

    size++
    def pop()
    # and
    size--
    def to_s()
    

    like

    size + +def pop()
    # and 
    size - -def to_s()
    

    Ruby syntax is expression-oriented and method definition is expression in Ruby. Method definition expressions (def pop() and def to_s()) are evaluated to nil (in your code you actually define method pop inside push method body and to_s inside pop method body). And this is why you get NoMethodError: undefined method +@' for nil:NilClass error – it evaluates expression size + +nil and nil does not define unary plus operator. In this expression first + is an Fixnum addition operator (size is Fixnum), and second + is unary plus operator of nil (result of def pop() expression).

    Use += 1 and -= 1 instead of ++ and --. Your code should look like this:

    class Stack
        attr_accessor :top
    
        def size
                @size
        end
    
        def push(node)
            if node && node.next
                node.next = top
                top = node
            end
            @size += 1 # @size, not `size` because you have `size` getter and you cannot modify size with getter method
        end
    
        def pop()
            if top != nil
                top = top.next
            end
            @size -= 1
        end
    
        def to_s
            if top != nil
                temp = top
                while temp != nil
                    puts temp.value
                    temp = temp.next
                end
            else
                puts "The stack is empty"
            end
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am starting to learn ruby and am trying to figure out a way
I'm just starting to learn Ruby and have a problem with encoding; require 'rubygems'
I'm coding a simple text game to learn ruby. The hash has to be
I'm starting to learn Ruby. I read that arguments where passed by reference to
I am just starting to learn Ruby (to eventually move to RoR), but I
I am just starting to learn Ruby on Rails and have a newbie question
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read
I'm starting to learn Ruby on Rails but this question is bugging me. As
I am starting to learn ruby on rails, like it so far. But I
i'm starting to learn ruby on rails using this guide : getting_started , 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.