Send Parameters for Control
Ruby is very flexible, and there are many things you can do in Ruby that are impossible in more restrictive languages.
Ordinary methods
Most methods in classes are instance methods and are called on instances of a class (instead of the class itself). In most cases, you know the name of a method and the number of parameters it takes in, and can call it directly. For example, if you have the following class:
class Dog
def initialize(name)
@name = name
end
def greet(person)
puts "Hi #{person} I'm #{@name}"
end
end
...you can create a dog, and call the greet
method:
doggy = Dog.new("Max")
doggy.greet("Jim")
..which will output:
Hi Jim I'm Max
parameters
Usually, methods take in a specific number of parameters. But you can also create a method that can take in any number of parameters, from 0 and up. You can do this by adding a *
at the beginning of the parameter, so multiple parameters can be passed in will be bundled into one array. For example, you can add a new method to the above class for greeting multiple people:
def greet_many(*people)
people.each {|person| greet(person)}
end
This code uses .each to go through *people
and call greet
on each person
. You can now call greet_many
and pass in any number of parameters:
doggy = Dog.new("Max")
doggy.greet_many("Jim", "Sam", "Sarah")
This will output:
Hi Jim I'm Max Hi Sam I'm Max Hi Sarah I'm Max
Above, the parameter *people
used a *
to bundle multiple variables into one array. You can do the reverse when calling a method and use a *
to split an array into multiple variables. Given the following method:
def greeter(title, first_name, last_name)
puts "Hello #{title} #{first_name} #{last_name}"
end
...You can do the following with an array:
person_ar = ["Dr.", "Sarah", "Jones"]
greeter(*person_ar)
This will split person_ar
into individual variables, so greeter
outputs:
Hello Dr. Sarah Jones
Default parameters
In Ruby you can also define default values for parameters with an =
. You can modify the original greet
method so it can be called without any parameters:
def greet(person="Whoever")
puts "Hi #{person} I'm #{@name}"
end
Code that calls such a method can override these values, but otherwise the method will go with the defaults:
doggy = Dog.new("Max")
doggy.greet("Jim")
doggy.greet
This outputs:
Hi Jim I'm Max Hi Whoever I'm Max
send
In rare cases, you may want to call a method dynamically based on other code or even user input. Instead of directly calling a method, you call send
and pass it a string or symbol for the method name. For example, let's say you have a simple method:
def hello
puts "Hello World!"
end
If you don't want to call it directly, you can use send
and pass in the name of the method:
send(:hello)
This outputs:
Hello World!
You can call send on any object. For example:
doggy = Dog.new("Max")
doggy.send("greet")
this outputs the same thing as doggy.greet
:
Hi Whoever I'm Max
The first parameter of send
is for the name of the method, but it can take in additional parameters afterwards which will be used as parameters of the method:
doggy = Dog.new("Max")
doggy.send("greet", "Jack")
This outputs:
Hi Jack I'm Max
Challenge
The last challenge involved creating a simple Model/database for storing products, but we don't want to give everyone direct access to it. Instead let's create a Controller
class that will take in text and call the appropriate methods.
Create a class called Controller with 3 instance methods:
show(id)
- this should pass theid
to your Model'sfind
method and print out the product that is returned. (Useputs
to print with a newline.)create(*params)
- this should call your Model'screate
method with the given parameters. It should then print out "Product created".process(input)
- this will take in a string where the first word is a method name of your controller and the remaining words are variables for that controller method. Call the given controller method and pass in the variables!
Helpful String methods
Since you'll be dealing with Strings, you can review the String page for basic usage. It showed that a number can be converted to a String with the .to_s
method. Strings can also be converted to integers with the to_i
method:
"32".to_i #=> 32
You can split a string into an array of strings with split
. Just pass in the string or pattern you want to split the string by. For example, you can split a sentence into an array of words by splitting at every space " ":
"Pack my box with five dozen liquor jugs".split(" ")
#=> ["Pack", "my", "box", "with", "five", "dozen", "liquor", "jugs"]
See the official String docs for all the String methods Ruby comes with.
Challenge
Create a controller class as described above.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
The following Ruby code returns half of the maximum number in an array:
def max(ar)
ar.max / 2
end
This code can now be passed an array and it will return the maximum number in it divided by 2:
max([1, 3, 20]) #=> 10
The parameter in max
is then modified as follows:
def max(*ar)
ar.max / 2
end
Rewrite the above call to max
so it works with the modified method. (Pass in the same numbers in the same order.)
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Ahmad Abbas Zainol
Sep 13, 1:08 PMI have problem to understand this challenge. What should I do for the third question?