summaryrefslogtreecommitdiffhomepage
path: root/samples/00_intermediate_ruby_primer/app/06_functions.txt
blob: 6ee8da1f0cacac2561fb32ff69499cf4dacc1dbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# ====================================================================================
# Functions
# ====================================================================================

# The last statement of a function is implictly returned. Parenthesis for functions
# are optional as long as the statement can be envaluated disambiguously.

puts ''
puts ''
puts '================================'
puts ''

# ====================================================================================
# Functions single parameter
# ====================================================================================

def add_one_to n
  n + 1
end

puts add_one_to(3)

# ====================================================================================
# Functions with default parameter values
# ====================================================================================

def function_with_default_value v = 10
  v * 10
end

puts "passing three: #{function_with_default_value(3)}"
puts "passing nil: #{function_with_default_value}"

# ====================================================================================
# Nil default parameter value and ||= operator.
# ====================================================================================

def function_with_nil_default_with_local a = nil
  result   = a
  result ||= "or equal operator was exected and set a default value"
end

puts "passing 'hi': #{function_with_nil_default_with_local 'hi'}"
puts "passing nil: #{function_with_nil_default_with_local}"

puts ''
puts '================================'
puts ''
puts ''