0% found this document useful (0 votes)
33 views8 pages

In This Lecture, We Will Discuss : Functions / Methods

This document discusses functions and methods in Ruby. It explains that functions are defined outside classes while methods are defined inside, but in Ruby all functions are technically methods. It covers how to define and call methods, including optional parentheses, returning values, and default arguments. The document also explains how to make methods more expressive using question marks, exclamation points, and the splat operator to designate parameters.

Uploaded by

JITENDRA TIWARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views8 pages

In This Lecture, We Will Discuss : Functions / Methods

This document discusses functions and methods in Ruby. It explains that functions are defined outside classes while methods are defined inside, but in Ruby all functions are technically methods. It covers how to define and call methods, including optional parentheses, returning values, and default arguments. The document also explains how to make methods more expressive using question marks, exclamation points, and the splat operator to designate parameters.

Uploaded by

JITENDRA TIWARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

In this lecture, we will discuss…

² Functions / Methods
•  Definitions
•  How do you call them?
•  What and how do they return?
•  Default args
² How to make methods more expressive
² What is “splat”
Functions and Methods
² Technically, a function is defined outside of a class and
a method is defined inside a class
² In Ruby, every function/method has at least one class it
belongs to
•  Not always written inside a class

Conclusion: Every function is really a method in


Ruby
Methods
² Parentheses are optional
both when defining and
calling a method
•  Used for clarity
Return
² No need to declare type of
parameters
² Can return whatever you
want
² return keyword is
optional (last executed line
returned)
Expressive Method Names
² Method names can end
with:
•  ‘?’ - Predicate methods
•  ‘!’ - Dangerous side-
effects (example later by
strings)
Default Arguments
² Methods can have default
arguments
•  If a value is passed in –
use that value
•  Otherwise – use the
default value provided

Ternary operator:
condition ? true : false
Splat
² * prefixes parameter
inside method
definition
•  Can even apply to
middle parameter,
not just the last
Summary
² There is no need to declare parameter type passed in or
returned (dynamic)
² return is optional – the last executable line is
“returned”
² You can construct methods with variable number of
arguments or default arguments
What’s next?
² Blocks

You might also like