[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie Question new/initialize

Eric-Roger Bruecklmeier

11/4/2003 8:40:00 AM

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.

5 Answers

ts

11/4/2003 10:41:00 AM

0

>>>>> "E" == Eric-Roger Bruecklmeier <news01@eric-bruecklmeier.de> 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 ...


--

Guy Decoux

Robert Klemme

11/4/2003 10:48:00 AM

0


"Eric-Roger Bruecklmeier" <news01@eric-bruecklmeier.de> schrieb im
Newsbeitrag news:3FA765CB.2010805@eric-bruecklmeier.de...
> 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

Robert Klemme

11/4/2003 12:24:00 PM

0


"ts" <decoux@moulon.inra.fr> schrieb im Newsbeitrag
news:rfcd6c8fmbi.fsf@moulon.inra.fr...
> >>>>> "E" == Eric-Roger Bruecklmeier <news01@eric-bruecklmeier.de>
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

Eric-Roger Bruecklmeier

11/4/2003 3:58:00 PM

0


>
> 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.

aero6dof

11/4/2003 6:29:00 PM

0

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

Eric-Roger Bruecklmeier <news01@eric-bruecklmeier.de> wrote in message news:<3FA765CB.2010805@eric-bruecklmeier.de>...
> 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