What Ruby Version?

17 views
Skip to first unread message

Lex Peters

unread,
Nov 9, 2016, 3:42:06 PM11/9/16
to Understanding Computation discussion
What version of Ruby does this book use?

When I write the following code.

https://gist.github.com/anonymous/e3f55332903944509c1a4820b71d686b

class Number

    def initialize(n)
        @value = n
    end

    def to_s
        @value.to_s
    end

    def inspect
        "«#{self}»"
    end
end

class Add

    def initialize (left, right)
        @left = left
        @right = right
    end

    def to_s
        "#{@left} + #{@right}"
    end
    def inspect
        "«#{self}»"
    end
end

class Multiply
    def initialize (left, right)
        @left = left
        @right = right
    end

    def to_s
        "#{@left} * #{@right}"
    end
    def inspect
        "«#{self}»"
    end
end

puts Add.new(
    Multiply.new(Number.new(1), Number.new(2)),
    Multiply.new(Number.new(3), Number.new(4))
)

puts Number.new(5)

I receive these errors.

./uc0.ruby:34:in `initialize': wrong number of arguments (given 1, expected 0) (ArgumentError)
    from ./uc0.ruby:34:in `new'
    from ./uc0.ruby:34:in `<main>'

Murray Steele

unread,
Nov 10, 2016, 4:58:34 AM11/10/16
to Lex Peters, Understanding Computation discussion
Hi Lex,

The code in the book should work with any version of ruby.  2.0 was the most current at the time the book was published, so any 2.x version should work.  There’s nothing in the snippet you’ve posted that looks like it wouldn’t work in any version of ruby though.

I think the problem you’re seeing is that the code in your email and the code in your gist snippet are different.  Your email has initialize methods in it for Number, Add, and Multiply, but the code in the gist doesn’t.  Without the custom initialize methods the Number, Add, and Multiply classes will fall back to the default initialize method defined on all objects.  This default initializer doesn’t take any arguments and that’s why you’re getting the “`initialize': wrong number of arguments (given 1, expected 0)” error.

Hope this helps!

Cheers,

Murray



--
You received this message because you are subscribed to the Google Groups "Understanding Computation discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to computationbook+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Francis Fish

unread,
Nov 10, 2016, 7:08:47 AM11/10/16
to Murray Steele, Lex Peters, Understanding Computation discussion
It *might* be that older versions of Ruby don't like spaces before the opening bracket on the definition of methods.

I pasted the code into a file and ran Ruby 2.2.4 and it ran fine.


Thanks and Regards,

Francis

07764 225 942

Pharmarketeer is a registered company in England and Wales 06940361, registered office 64 Westbank Road, Birkenhead, Merseyside, CH42 7JP

"So when targets seem stupid, arbitrary and unfair it's because they are. The only way to improve is to look at the whole system people are operating with, the basic tools, their training, how much initiative they are allowed, are you measuring the right things (more about that later) and then you can improve. But it's the system you improve, not the people you beat into performing even worse." Unicorns in the mist

Lex Peters

unread,
Nov 10, 2016, 12:07:37 PM11/10/16
to Understanding Computation discussion, sex...@gmail.com
Murray, the code on Gist is the one I am having problems with.  The one in my post was my solution from help on IRC. 

I would like to resolve the problems.  It is making it difficult to study.

Thanks, Lex
To unsubscribe from this group and stop receiving emails from it, send an email to computationbo...@googlegroups.com.

Tom Stuart

unread,
Nov 10, 2016, 12:38:22 PM11/10/16
to Lex Peters, Understanding Computation discussion
Hi Lex,

On Thu, Nov 10, 2016 at 5:07 PM, Lex Peters <sex...@gmail.com> wrote:
Murray, the code on Gist is the one I am having problems with.

The gist does not contain all of the necessary code from the book, which is why it doesn’t work.

The confusion is likely to be because the code in the book builds up incrementally, so any individual piece of code is unlikely to work in isolation. In this particular case there are method definitions on page 24 that look like your gist, but they depend on having already evaluated the class definitions on page 23 (“class Number < Struct.new(:value)” etc) which provide the correct constructors for the Number, Add and Multiply classes. The code on page 24 reopens these existing classes to add more methods, and the same pattern continues throughout the rest of the chapter to add #reducible?, #reduce, #evaluate etc.

I apologise for the problems this has caused you. If it helps, the final working version of each of these classes can be found at https://github.com/tomstuart/computationbook/tree/master/the_meaning_of_programs/syntax, and all the other code in the book can be found elsewhere in the same repository (organised by chapter), so I hope that will be useful if you have any similar problems later on.

If not, please do come back and ask more questions!

Cheers,
-Tom

Lex Peters

unread,
Nov 15, 2016, 2:42:09 PM11/15/16
to Understanding Computation discussion, sex...@gmail.com
Tom, thanks for the help.  That is exactly where I was struggling.  I understand now and it is all working.

Thanks, Lex
Reply all
Reply to author
Forward
0 new messages