zaagan
12/13/2019 - 6:49 PM

Ruby Basics - Optional parameters

Ruby Basics - Optional parameters


def greet(name, informal: false, shout: false)
  greeting = if informal then "hi" else "hello" end
  message = "#{greeting} #{name}"
  if shout
    message.upcase
  else
    message
  end
end

puts greet("Ozesh", shout: true)
puts greet("Ozesh", shout: false, informal: true)


# HELLO OZESH
# hi Ozesh

# Optional Parameters
def optional_first(first = 1, mid, last)
    # Statement
end

def optional_last(first, mid, last = 1)
    # Statement
end

def greet(name, informal = false)
    if informal
        "hi #{name}"
    else
        "hello #{name}"
    end
end
puts greet('Ozesh')
puts greet('John',true)

# hello Ozesh
# hi John