There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
rev=false is a parameter and it’s default when no input is given
so when we say if rev=true I am confused - is rev=false true when there is no input? and rev=false is false when we give that parameter a value?
is the case when there is no second positional argument. It would make little sense to call the method, method(something, false) since that is already the default.
However, if we include true as a second argument, the return will be reversed.
I am stuck with this exercise as well. Not sure I understood the instructions correctly. What do they mean by after your sort! call…which one are they referring too? what part of the code? the top part or the one under my arrays numbers? I am confused.
Thank you in advance for any help!
1.
Instructions:
1/After your .sort! call, add an if-else statement. If rev is true, call reverse! on arr , else return arr .
Keep your numbers array and the puts statement so that you can see your work in action! Code:
def alphabetize(arr, rev=false)
if rev
arr.sort!{|item1, item2| item2<=>item1}
else
arr.sort!{|item1, item2| item1<=>item2}
end
end
numbers=[10, 12, 35, 17]
numbers.sort!
if rev==true
numbers.reverse!
else
return arr
end
puts numbers
I think that the instructions are a little bit confusing, because it says:
After your .sort! call, add an if-else statement. If rev is true, call reverse! on arr, else return arr.
The else statement is the most confusing part.
So:
This means exactly the same as " else return arr. ", but if you actually use return here, you get no results.
I am having difficulties to understand return, but your explanation helped me to understand it better now.
Still,
I’m still stuck on this (possibly bc English is not my first language).
When adding arrafter closing your if block, shouldn’t the def method get only this last information?
When we look at the first example, there is a return in the else clause but none in the if clause. We account for that by not having an else clause, only an if, with its reverse action taking place immediately on the array. Once that action is complete we arrive at the one return that hands back either result. Note that since it is the last line in the method block, return is implicit so we don’t need to write it.
of the method, just as a block also has an implicit return. The last line is an expression (a value).
def pi
Math::PI.round(5)
end
puts pi
# 3.14159
We learn at some point that this is the way Ruby prefers variables to be defined.
Let’s consider an example where implicit return is used extensively…
def pi
Math::PI.round(5) # here
end
class Circle
def initialize(radius)
@radius = radius
end
def radius
@radius # here
end
def circumference
2 * pi * radius # here
end
def area
pi * radius ** 2 # here
end
end
def unit_circle
unit_circle = Circle.new(1)
# and here
<<-OUTPUT
Unit Circle
#{'=' * 30}
Radius: #{unit_circle.radius} unit
Circumference: #{unit_circle.circumference} units
Area: #{unit_circle.area} square units
#{'=' * 30}
OUTPUT
end
puts unit_circle
That is the output of the lesson checker. No cause for confusion. A lot of times the SCT (Submission correctness Test) built into the lesson will run our code with preset inputs.