A method is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program. Other languages sometimes refer to this as a function. A method may be defined as a part of a class or separately.
Create new array and iterate through array squaring each element before pushing it to new array and return new array
# created a new empty array arr.# iterate through the other array array which is passed as an argument, squaring each element before pushing it (using <<) into our new array arr.
# Finally we return the newly created array arr by simply writing arr as the final line in the method block.defsquare_array(array) arr = [] array.each { |i| arr << i **2 }return arrend
Return a name in a sentence
# a badge_maker method that, when provided a persons name, will create and return this message:defbadge_maker(name)Return “Hello, my name is #{name}.”end
Takes an array of names and returns an array
# write a batch_badge_creator method that takes an array of names as an argument and returns an array of badge messages:
defbadge_maker(speakers) speakers.map { |speaker| “Hello, my name is #{speaker}.” }end
Assign each argument with only 1 parameter
# Write a method called assign_rooms that takes the list of speakers and assigns each speaker to a room. Make sure that each room only has one speaker:
defassign_rooms(speakers)Speakers.each_with_index.map do| speaker, room | “Hello,#{speaker}! "You’ll be assigned to room #{room + 1}!”endend
Outputs a messages to screen
# Create a method called printer that will output first the results of the batch_badge_creator method and then of the assign_rooms method to the screen:
defprinter(speakers) batch_badge_creator(speakers).each do|badge|puts badgeend assign_rooms(speakers).each do|assignment|puts assignmentendend
Keep place and show everyone their current location in line
# Build the line method that shows everyone their current place in the line. If there is nobody in line, it should say "The line is currently empty.".
# Build a method that a new customer will use when entering the deli. The take_a_numbermethod should accept two arguments, the array for the current line of people (katz_deli), #and a string containing the name of the person wishing to join the line. The method should return the persons name along with their position in line. Top-Tip: Remember that #people like to count from 1, not from 0 ("zero") like computers. Build the now_serving method which should call out (i.e. puts) the next person in line and then remove them #from the front. If there is nobody in line, it should call out (puts) that "There is nobody waiting to be served!".
defline(katz_deli)if katz_deli.length ==0puts “The line is currently empty.”else line_list = “The line is currently:” katz_deli.each_with_index do| person, index | line_list << “ #{index + 1). #{person}”endputs line_listendenddeftake_a_number(katz_deli, person) katz_deli << personputs “Welcome,#{person}. You are number #{katz_deli.length} in line.”enddefnow_serving(katz_deli)if katz_deli.length ==0puts “There is nobody waiting to be served!”elsePuts “Currently serving #{katz_deli[0]0} katz_deli.shiftendend
Takes an argument(array) of string elements and convert it into a string
# Write a method oxford_comma that takes an argument array of string elements # and converts it into a string using the Oxford comma.defoxford_coma(array)if array.length ==1 array.joinelsif array.length ==2 array.join( “ and “)else array.length >=3 array[0..-2].join(“, “) + “,and “ + array[-1]endend
Reverse and return each word in a sentence
# Write a method called reverse_each_word that takes in a string argument of a sentence # and returns that same sentence with each word reversed in place.defreverse_each_word(string) string.split(“ “).collect { |word| word.reverse }.join(“ “)end
Only return greeting if block starts with "T" or "t"
# Only put out a greeting if the word we pass into the block starts with the letter "T".defhello_t(array) i =0 new_arr = []while i < array.lengthif (array[i].start_with?(“T”) || array[i].start_with?(“t”))yield(array[i]) new_arr << array[i]end i +=1end new_arrend# calling the method hello_thello_t([“Tim”, “Tom”, “Jim”]) { |name|puts “Hi,#{name}” }
Pass each element of a collection to a block
#Define a method that uses yield and a while loop to pass each element of a collection to a block.defmy_each(array) i =0while i < array.lengthyield array[i] i +=1end arrayend
Iterate over an array and yielding each member of the array into a block
# We are using a while loop to iterate over an array and yielding each member of the array in turn to a block:defhello(array) i =0while i < array.lengthyield(array[i]) i +=1endend#We would call our method like this:hello([“Tim”, “Tom”, “Jim”]) { |name|puts “Hi,#{name}” }
Push the return values using yield into empty array and return new array at the end
# We are setting a variable, collection, equal to an empty array.# Then, inside our while loop, we push the return value of using yield(array[i]) into that collection array and return that new collection at the end:
defhello(array) i =0 collection = []while i < array.length collection <<yield(array[i]) i +=1end collectionend
Yields members of an array to a block
# Build a method that yields members of a collection to a block.defmy_collect(collection) mod_collection = [] i =0while i < collection.length mod_collection <<yield(collection[i]) i +=1end mod_collectionend