In: Computer Science
In Ruby, methods are not what is called first-class citizens. However, procs (lambdas as well) and block logic in Ruby are. For each of the following descriptions below give an example of a proc in Ruby that satisfies the condition. There should be an example for EACH QUESTION.
QUESTION b)The first class object may be stored in variables (i.e. it may have a symbolic name). Show examples of defining and using named functions.
->You'll often have a piece of code that needs to be executed many times in a program. Instead of writing that piece of code over and over, there's a feature in most programming languages called a procedure, which allows you to extract the common code to one place. In Ruby, we call it a method.
def say
# method body goes here
end
There's a comment in the method body to show you where the logic for the method definition will go.
Suppose we had the following code in a file named say.rb. Create this file and type these examples along.
puts "hello"
puts "hi"
puts "how are you"
puts "I'm fine"
->Notice how we've duplicated the puts many times. We'd like to have one place where we can puts and send that one place the information we want to puts. Let's create a method definition to do that.
def say(words)
puts words
end
say("hello")
say("hi")
say("how are you")
say("I'm fine")
-> We call (or invoke) the method by typing its name and passing in arguments. You'll notice that there's a (words) after say in the method definition. This is what's called a parameter. Parameters are used when you have data outside of a method definition's scope, but you need access to it within the method definition. If the method definition does not need access to any outside data, you do not need to define any parameters.
-> One of the benefits that methods give us is the ability to make changes in one place that affect many places in our program. Suppose we wanted to add a . at the end of every string we send to the say method. We only have to make that change in one place.
def say(words)
puts words + '.' ## <= We only make the change here!
end
say("hello")
say("hi")
say("how are you")
say("I'm fine")
Default Parameter:
When you're defining methods you may want to structure your method definition so that it always works, whether given parameters or not. Let's restructure our say method definition again so that we can assign a default parameter in case the caller doesn't send any arguments.
def say(words='hello')
puts words + '.'
end
say()
say("hi")
say("how are you")
say("I'm fine")
You'll notice that say() prints hello. to the console. We have provided a default parameter that is used whenever our method is called without any arguments.
Method Definition and Local Variable Scope
Before moving on to the next topic on methods, let's take a moment to discuss the concept of local variable scope within a method definition. A method definition creates its own scope outside the regular flow of execution. This is why local variables within a method definition cannot be referenced from outside of the method definition.
a = 5
def some_method
a = 3
end
puts a
What's the value of a? Still 5, because method definitions create their own scope that's entirely outside of the execution flow.