Checking the parameters "type"

  • Thread starter Imobach González Sosa
  • Start date
I

Imobach González Sosa

Hi all,

I've trying to find out how to check the class of a parameter. Right, I
talking about this:

class TestClass
# arg1 must be String instance
def myMethod(arg1, arg2)
# Code, code and more code.
end
end

I think that I could solve in this way:

class TestClass
# arg1 must be a String instance
def myMethod(arg1, arg2)
if not arg1.kind_of?(String)
# Error! Some code must be added!
end
end
end

Right, we are going to suppose now that I want to add an Integer as attribute
(myAttribute):

class TestClass
attr_writer :myAttribute
...
end

This attribute must be writable. But, there's a little trouble:
How could I assure that the parameter passed to myAttribute= method is
Integer? Must I implement the method by myself?

class TestClass
def myAttribute=(value)
if not myAttribute
end
end

Can it be automatized in some way?

Thank you all very much!
 
G

gabriele renzi

il Fri, 30 Jan 2004 05:44:26 +0900, "Imobach González Sosa"
Hi all,

I've trying to find out how to check the class of a parameter. Right, I
talking about this:

look on raa for types.rb and strongtyping modules. These will make it
easy for you to check types and will give you some more goodies ( like
kind of MMD )
 
J

Josef 'Jupp' SCHUGT

Hi!

* Imobach González Sosa:
Hi all,

I've trying to find out how to check the class of a parameter. Right, I
talking about this:

class TestClass
# arg1 must be String instance
def myMethod(arg1, arg2)
# Code, code and more code.
end
end

I think that I could solve in this way:

class TestClass
# arg1 must be a String instance
def myMethod(arg1, arg2)
if not arg1.kind_of?(String)
# Error! Some code must be added!
end
end
end

Try this:

class TestClass
def myMethod(arg1, arg2)
unless arg1.class == String
raise ArgumentError.exception('first argument must be string')
end
puts arg1, arg2
end
end

Test:

test = TestClass.new
test.myMethod('foo', 'bar')
test.myMethod(1, 'bar')


How could I assure that the parameter passed to myAttribute= method
is Integer?

Why not? It is easy to define a setter:

class TestClass
def myAttribute=(value)
if value.class <= Integer
@myAttribute = value
else
raise ArgumentError.exception('assignment requires integer')
end
end

def myAttribute
@myAttribute
end
end

I did add a getter so that testing is possible.

test = TestClass.new
test.myAttribute = 10
puts test.myAttribute
test.myAttribute = 'foo'
puts test.myAttribute

Question to community: Raise an ArgumentError or a TypeError?

Josef 'Jupp' SCHUGT
 
I

Imobach González Sosa

El Jueves, 29 de Enero de 2004 22:44, gabriele renzi escribió:
il Fri, 30 Jan 2004 05:44:26 +0900, "Imobach González Sosa"



look on raa for types.rb and strongtyping modules. These will make it
easy for you to check types and will give you some more goodies ( like
kind of MMD )

Thank you gabriele and Jupp for your answers. At this moment, I'm trying
StrongTyping, and it seems to work just fine for my purposes.

See ya!
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top