Blocks and Sorting Flashcards
Write the syntax for a simple method
def greeting
puts “Well helloooo”
end
If you write out a method where you want it to write a greeting message to not just one friend, but multiple friends, how could you do this?
def greeting (friend*)
puts “Hello there, #{friend}!”
end
greeting(“Mohit”, “Seb”)
Create a block in the two possible ways
2.times do
puts “Hello”
end
2.times { puts “Bye” }
Use your knowledge on methods to sort and then reverse the an array based on whether the user calls the method with one or two paramaters
def abc (numbers, rev=false) arr.sort! if rev return arr.reverse! else return arr end end
numbers = [3, -5, 1, 100] puts abc (numbers) puts abc (numbers, numbers)
How would edit the following array with numbers, so that the numbers are placed in the way from smallest to largest?
my_array = [3, 2, 5, 1, 4]
print my_array.sort!
How would you edit the following array so it is in opposite order, so not abc order, but z-a
fruits = [‘orange’, ‘apple’, ‘pear’]
fruits.sort! { |firstFruit, secondFruit|
secondFruit<=>firstFruit}
print fruits