Newbie Question new/initialize

  • Thread starter Eric-Roger Bruecklmeier
  • Start date
E

Eric-Roger Bruecklmeier

Hi rubyists,

i try so solve the following problem:


class Foo

def initialize(par)

if par 'not good'
return nil <= ???
else

do something usefull

end

end


The goal is to create an instance of NilClass instead of Foo if par has
certain attributes:

test = Foo.new(good)
p test.class -> Foo

test = Foo.new(bad)
p test.class -> NilClass

Any suggestions how this can be done?

Thanx!

Eric.
 
T

ts

E> Any suggestions how this can be done?

The good question is perhaps why you want to do this ? ::new normally
return an object of its class

svg% cat b.rb
#!/usr/bin/ruby

class A
def self.new(par)
super if par == 12
end

def initialize(par)
@a = par
end
end

p A.new(1).class
p A.new(12).class
svg%

svg% b.rb
NilClass
A
svg%

probably many persons will be surprised by this behaviour ...
 
R

Robert Klemme

Eric-Roger Bruecklmeier said:
Hi rubyists,

i try so solve the following problem:


class Foo

def initialize(par)

if par 'not good'
return nil <= ???
else

do something usefull

end

end


The goal is to create an instance of NilClass instead of Foo if par has
certain attributes:

test = Foo.new(good)
p test.class -> Foo

test = Foo.new(bad)
p test.class -> NilClass

Any suggestions how this can be done?

Thanx!

Exceptions are the appropriate means:

class Foo
def initialize(par)
raise ArgumentError, "Not ok: #{par}" unless /ok/ =~ par
# do something useful
end
end

begin
x = Foo.new "par"
# work with x
rescue ArgumentError => e
# handle error
end

Of course you can encapsulate it in a method and return nil as error
handling. But then you will have to check for nil in other places. It
depends on the application whether that is ok or not.

Regards

robert
 
R

Robert Klemme

ts said:
writes:

E> Any suggestions how this can be done?

The good question is perhaps why you want to do this ? ::new normally
return an object of its class

svg% cat b.rb
#!/usr/bin/ruby

class A
def self.new(par)
super if par == 12
end

def initialize(par)
@a = par
end
end

p A.new(1).class
p A.new(12).class
svg%

svg% b.rb
NilClass
A
svg%

probably many persons will be surprised by this behaviour ...

Personally I don't like fiddling with new as this certainly violates POLS
as you mention. An explicite factory method is a much clearer way to
achieve the same goal IMHO.

Regards

robert
 
E

Eric-Roger Bruecklmeier

The good question is perhaps why you want to do this ? ::new normally
return an object of its class

svg% cat b.rb
#!/usr/bin/ruby

class A
def self.new(par)
super if par == 12
end

def initialize(par)
@a = par
end
end


Thanx that's it!


Eric.
 
A

Alan Chen

A factory method might be a nice way to accomplish what you're
looking for.

Eric-Roger Bruecklmeier said:
Hi rubyists,

i try so solve the following problem:


class Foo

def self.checked_new(par)
if par 'not good'
return nil <= ???
end
return Foo.new(par)
end
def initialize(par)
do something useful
end

end



class Foo
def self.checked_new(par)
if par 'not good'
return nil
else
return Foo.new
end
end
end
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top