Sorting problem

<meta http-equiv="content-type" content="text/html; 

charset=ISO-8859-1">

Hello,

I do the codeacademy course and I have sort a array.

So I have this :

def alphabetize (arr, rev=false)
    arr.sort!
    if rev == true
        arr.reverse!
    end
end

array = [ "c", "b", "a"]
puts alphabetize(array)
array2 = [ 3,2,1]
puts alphabetize(array2)

but now I see this error message :

Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter.

Roelof



Make sure you return the array at the end of your method

def alphabetize (arr, rev=false)
arr.sort!
if rev == true
arr.reverse!
end
arr
end

On Tue, Nov 18, 2014 at 5:25 PM, Roelof W. [email protected] wrote:

end

alphabetizing an array when it doesn’t receive a second parameter.

Roelof


George D.
Software Engineer

+44 (0)333 240 2222

[image: Rentify]
6-8 Long Lane, London EC1A 9HF
www.rentify.com

Thanks, that did the trick.

Roelof


George D. schreef op 18-11-2014 18:32:
Make sure you return the array at the end of your method

def alphabetize (arr, rev=false)
?? ?? arr.sort!
?? ?? if rev == true??
?? ?? ?? ?? arr.reverse!
?? ?? end
?? ?? arr
end

On Tue, Nov 18, 2014 at 5:25 PM, Roelof Wobben <[email protected]> wrote:
Hello,

I do the codeacademy course and I have sort a array.

So I have this :

def alphabetize (arr, rev=false)
?????? arr.sort!
?????? if rev == true
?????????????? arr.reverse!
?????? end
end

array = [ "c", "b", "a"]
puts alphabetize(array)
array2 = [ 3,2,1]
puts alphabetize(array2)

but now I see this error message :

Oops, try again. It looks like your method doesn't default to alphabetizing an array when it doesn't receive a second parameter.

Roelof






--
George??Drummond
Software Engineer

+44 (0)333 240 2222

??

Rentify
6-8 Long Lane, London EC1A 9HF


On Tue, Nov 18, 2014 at 9:43 AM, Roelof W. [email protected] wrote

end
This could be shorter and more readable as

def alphabetize (arr, rev=false)
arr.sort!
arr.reverse! if rev
arr
end

or more simply (if possibly less readable)

def alphabetize (arr, rev=false)
arr.sort!.tap{ arr.reverse! if rev }
end

FWIW,

On Tue, Nov 18, 2014 at 6:25 PM, Roelof W. [email protected] wrote:

if rev == true

NEVER compare a value with a boolean constant! This will not improve
readability but introduce subtle errors - especially in languages like
Ruby which have multiple values for “true” and “false”. Even in
other languages that - like Java - have only one value for “true” and
one for “false” this is unnecessary. Boolean logic is there for a
reason.

Kind regards

robert