Blocks and Sorting Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Write the syntax for a simple method

A

def greeting
puts “Well helloooo”
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

def greeting (friend*)
puts “Hello there, #{friend}!”
end
greeting(“Mohit”, “Seb”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create a block in the two possible ways

A

2.times do
puts “Hello”
end

2.times { puts “Bye” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would edit the following array with numbers, so that the numbers are placed in the way from smallest to largest?

A

my_array = [3, 2, 5, 1, 4]

print my_array.sort!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you edit the following array so it is in opposite order, so not abc order, but z-a

A

fruits = [‘orange’, ‘apple’, ‘pear’]
fruits.sort! { |firstFruit, secondFruit|
secondFruit<=>firstFruit}
print fruits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly