Re: Method parameters Type

From: Sreedhar K. [email protected]

Unlike Java why does not ruby specify the method parameter types.

Because, unlike Java, Ruby is not a strongly typed language.

How can a caller know what type of parameter is the method expecting.

Read the method’s documentation?

On Mon, Apr 7, 2008 at 4:28 PM, Mike B. [email protected]
wrote:

Because, unlike Java, Ruby is not a strongly typed language.

To be pedantic, both Java and Ruby are strongly typed. However, Java
is statically types, and Ruby is dynamically typed.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Avdi G. wrote:
| On Mon, Apr 7, 2008 at 4:28 PM, Mike B. [email protected]
wrote:
|> Because, unlike Java, Ruby is not a strongly typed language.
|
| To be pedantic, both Java and Ruby are strongly typed. However, Java
| is statically types, and Ruby is dynamically typed.
|

Ruby is?

irb(main):001:0> t = String.new
=> “”
irb(main):002:0> t.class
=> String
irb(main):003:0> t = 1
=> 1
irb(main):004:0> t.class
=> Fixnum
irb(main):005:0> exit

Doesn’t look like it to me, since I can change the type of a variable
with ease.


Phillip G.
Twitter: twitter.com/cynicalryan

Rule of Open-Source Programming #37:

Duplicate effort is inevitable. Live with it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf6jiUACgkQbtAgaoJTgL9AOgCfU5pDjiz+K1RP35cLhk9xZnlz
63AAoIKEeH+TUqC82uUoIHke3v+FpnzW
=9FdL
-----END PGP SIGNATURE-----

On Mon, Apr 7, 2008 at 5:12 PM, Phillip G.
[email protected] wrote:

Ruby is?

Doesn’t look like it to me, since I can change the type of a variable
with ease.

You’re confusing static typing and strong typing.

In a weakly-typed language, like C, it is possible to cast an integer
as a, for instance, a char*, and then call string functions like
sprintf() on it and the compiler will compile it, the runtime will run
it, and it will wreak whatever havoc you please. Most high-level
languages are strongly-typed, these days - neither Java or Ruby will
allow you to call a String method on an Integer. You can assign
whatever object you want to a variable in Ruby - hence dynamic
typing - but that object will only ever allow you to call supported
methods on it; otherwise you’ll get a NoMethodError. Hence strong
typing.

El Lunes, 7 de Abril de 2008, Phillip G.
escribió:>

Doesn’t look like it to me, since I can change the type of a variable
with ease.

No, look at the following example:

irb(main):001:0> text = "The number is: "
=> "The number is: "

irb(main):002:0> number = 25
=> 25

irb(main):003:0> puts text + number
TypeError: can’t convert Fixnum into String
from (irb):3:in `+’
from (irb):3
from :0

On 08.04.2008 00:53, Phillip G. wrote:

|
| methods on it; otherwise you’ll get a NoMethodError. Hence strong
| typing.
|

Thanks for the enlightenment. :slight_smile:

Another way to put it would be that Ruby’s variables are type-less,
while objects do have a specific type. While we’re at it: type !=
class. Basically the type is defined by all operations (aka methods)
usable on an instance - not the class it was created from.

Kind regards

robert

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Avdi G. wrote:
| On Mon, Apr 7, 2008 at 5:12 PM, Phillip G.
| [email protected] wrote:
|> Ruby is?
|>
|> Doesn’t look like it to me, since I can change the type of a variable
|> with ease.
|
| You’re confusing static typing and strong typing.
|
| In a weakly-typed language, like C, it is possible to cast an integer
| as a, for instance, a char*, and then call string functions like
| sprintf() on it and the compiler will compile it, the runtime will run
| it, and it will wreak whatever havoc you please. Most high-level
| languages are strongly-typed, these days - neither Java or Ruby will
| allow you to call a String method on an Integer. You can assign
| whatever object you want to a variable in Ruby - hence dynamic
| typing - but that object will only ever allow you to call supported
| methods on it; otherwise you’ll get a NoMethodError. Hence strong
| typing.
|

Thanks for the enlightenment. :slight_smile:


Phillip G.
Twitter: twitter.com/cynicalryan

Zmodem has bigger bits, softer blocks, and tighter ASCII.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf6pcwACgkQbtAgaoJTgL9trwCghb7DYangJvsq/ktpceHh12FD
004AnikbmUhG0gwWUJywu43qfbPa76sK
=b6S6
-----END PGP SIGNATURE-----

Avdi G. wrote:

To be pedantic, both Java and Ruby are strongly typed. However, Java
is statically types, and Ruby is dynamically typed

Indeed. I stand linguistically admonished. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs