Subscribe to How-To Geek

Ruby Function (method) Syntax

The Ruby language makes it easy to create functions.

Function Syntax

def functionname(variable)
   return <value>
end

Examples

Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return statement.

def say_hello(name)
   var = ”Hello, ” + name
   return var
end

The return statement also can be shortened for very simple functions into a single line

def say_hello(name)
   return “Hello, ” + name
end

You can simplify the function further. The last expression that is evaluated is automatically returned by the method. For example:

def say_hello(name)
   “Hello, ” + name
end

This would return the same value as the prior functions.

To call a function

function param1, param2

or

function(param1,param2)

Example

puts say_hello(”Geek”)

| More
This article was originally written on 11/22/09 Tagged with: Ruby

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
mDNSResponder.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.