how do you know which class pars belong to?

  • Thread starter Leonardo Francalanci
  • Start date
L

Leonardo Francalanci

Hello,

I've found Ruby very interesting. I like it, but coming from a Java
background I'm having some problems when it comes to parameters and
function return values. When I program in Java, I just have a look at
the method definition and I know what classes I have to pass to the
method and what class the method will return. And, if I write something
wrong, the compiler will warn me. Now: how do you do it in Ruby? Do you
use a special notation to understand what classes are to be passed to
methods? And: when the parameters of a method change (in number and/or
type), how do you change the calling code accordingly?

Example:

def mymethod(customer, vendor)
<some code here>
end

What class do customer and vendor belong? To Customer and Vendor? Or to
Person? Or are they just strings?
What are the ruturn types?

And when you change the code to:

def mymethod(customer, vendor, times)
<some code here>
end

do you use "grep" (or any other IDE command) to find all the calling
code to change it?

Hope this post doesn't end in a flame "java vs ruby". I just want to
understand how people deal with this things, so that I can get the best
out of Ruby.
 
R

Robert Klemme

Leonardo Francalanci said:
Hello,

I've found Ruby very interesting. I like it, but coming from a Java
background I'm having some problems when it comes to parameters and
function return values. When I program in Java, I just have a look at
the method definition and I know what classes I have to pass to the
method and what class the method will return. And, if I write something
wrong, the compiler will warn me. Now: how do you do it in Ruby? Do you
use a special notation to understand what classes are to be passed to
methods? And: when the parameters of a method change (in number and/or
type), how do you change the calling code accordingly?

Example:

def mymethod(customer, vendor)
<some code here>
end

What class do customer and vendor belong? To Customer and Vendor? Or to
Person? Or are they just strings?

Well, types are not that important in Ruby. When we talk about "Duck
Typing" then we mean, that we pass some value and if it doesn't respond to
the messages that the method wants to send them we'll see that pretty
quickly. This works surprisingly good (surprising for people with a
static typing languages background).
What are the ruturn types?

That's where documentation comes into play. :)
And when you change the code to:

def mymethod(customer, vendor, times)
<some code here>
end

do you use "grep" (or any other IDE command) to find all the calling
code to change it?

That's one option. The other is to use tests extensively. If test
coverage is good (i.e. near 100%) then you will immediately notice all
places where this method is called. Or you use a default value for the
added parameter(s).

Kind regards

robert
 
B

Brian Schröder

Use good methodnames, document the method and use rdoc on your source files# Adds a new entry to the "customer kills vendor" relationship. ## customer and vendor should respond to persons_id.def customer_killed_vendor(customer, vendor) insert_into('customer_kills_vendor', customer.persons_id, vendor.persons_id)end
Then use unit tests to test your sourcecode.
What class do customer and vendor belong? To Customer and Vendor? Or to> Person? Or are they just strings?> What are the ruturn types?> > And when you change the code to:> > def mymethod(customer, vendor, times)> <some code here>> end> > do you use "grep" (or any other IDE command) to find all the calling> code to change it?
there are some refactoring tools available, but grep seems nice. Inyour example I'd use a default value:def mymethod(customer, vendor, times=1) <some code here>end
as it seems your expanding the method call to add some behaviour.
Just my 0.02€
regards,
Brian Schröder
-- Brian Schröderhttp://ruby.brian-schroeder.de/
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top