Ruby Optional Parameters and Default Values Like C# 4.0

Although I think as .NET Developers we can probably get by with just knowing C# or VB, what would be the fun in that? :)

We have to remember as C# Developers that a lot of the features in our language are being borrowed from many languages. Hence when I talked about C# 4.0 Optional Parameters and C# 4.0 Named Parameters, this is new to us and not new in general.

 

Ruby Optional Parameters and Default Values

I won't pretent to be a Ruby guru as I just started learning the language, but Ruby has optional parameters right now. A simple screenshot shows an example similar to the previous C# 4.0 tutorial. I can specify default values in Ruby which makes them optional parameters. Notice how city and state have default values and I can optionally provide a value that overrides the default value.

 

Ruby Optional Parameters

 

Pretty slick!

The thing I don't understand at first glance is why doesn't Ruby have named parameters? Ruby does have starred parameters and the neatness of hashes, but seems like named parameters has a place in the language.

Anyway, as you can see, optional parameters with default values is new to C# 4.0 but not a new language feature in general.

 

David Hayden

 

posted on Tuesday, June 02, 2009 12:34 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices