Recursion in Ruby

R

Redson

I'm learning ruby on my own and going through the normal academic
examples to try and get started in this language.

While trying to do the simple "powerset" problem that's so easy in
lisp/ml, I run into either a "stack too deep" or an outright segfault,
which I didn't expect from ruby. I haven't been able to find anything
on google about it. I'm using ruby 1.8.4 on linux x86.

with the following code:

class Array
def powerset
if !self
return []
end
hd,*tl = self
tl.powerset.each do |i|
i + (i.push(hd))
end
end
end

I get:
irb(main):001:0> require 'setops.rb'
=> true
irb(main):002:0> [1,2].powerset
SystemStackError: stack level too deep
from ./setops.rb:30:in `powerset'
from ./setops.rb:30:in `powerset'
from (irb):2
 
T

Tassilo Horn

Hi Brian,
class Array
def powerset
if !self
return []
end
hd,*tl = self
tl.powerset.each do |i|
i + (i.push(hd))
end
end
end

self will never be false or nil. If the instance was nil, you couldn't
call a instance method. ;-)

I think you want something like: if self.empty?

Bye,
Tassilo
 
R

Robert Klemme

Redson said:
I'm learning ruby on my own and going through the normal academic
examples to try and get started in this language.

While trying to do the simple "powerset" problem that's so easy in
lisp/ml, I run into either a "stack too deep" or an outright segfault,
which I didn't expect from ruby. I haven't been able to find anything
on google about it. I'm using ruby 1.8.4 on linux x86.

with the following code:

class Array
def powerset
if !self
return []
end
hd,*tl = self
tl.powerset.each do |i|
i + (i.push(hd))
end
end
end

I get:
irb(main):001:0> require 'setops.rb'
=> true
irb(main):002:0> [1,2].powerset
SystemStackError: stack level too deep
from ./setops.rb:30:in `powerset'
from ./setops.rb:30:in `powerset'
from (irb):2

Your recursion never terminates because !self is never true. Only false
and nil are treated as false all other values (including empty arrays)
are true in a boolean context. Also I'm not sure whether your method
has a proper return value.

Regards

robert
 
R

Robert Klemme

Tassilo said:
Hi Brian,
class Array
def powerset
if !self
return []
end
hd,*tl = self
tl.powerset.each do |i|
i + (i.push(hd))
end
end
end

self will never be false or nil. If the instance was nil, you couldn't
call a instance method. ;-)

That's not exactly true: nil has a lot of methods you can invoke
=> NilClass

You can even define your own methods:
=> "foo"
I think you want something like: if self.empty?

empty? is sufficient.

Kind regards

robert
 
T

Tassilo Horn

Hi Robert,
That's not exactly true: nil has a lot of methods you can invoke

It's not even "not exactly true" -- it's simply wrong. I had to do some
Java-stuff when I replied. Shame on me. :)
empty? is sufficient.

Jepp.

Bye,
Tassilo
 
R

Robert Klemme

Tassilo said:
Hi Robert,


It's not even "not exactly true" -- it's simply wrong. I had to do some
Java-stuff when I replied. Shame on me. :)

*ggg*

I was trying to be polite...

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top