0

I'm not really understanding the rev=false portion which comes after def alphabetize:

def alphabetize (arr, rev=false)

or the portion:

if rev

If someone can explain it to me in simple terms that'd be great. When I search this question up it pops up with help as far as how it works which I understand, however I don't get arr=false or the last line.

puts "Z-A: #{alphabetize(books, true)}"

I'm sure those learning thru CodeAcademy would appreciate the answer. This is the code for reference.

def alphabetize (arr, rev=false)
  if rev
    arr.sort { |item1, item2| item2 <=> item1 }
  else
    arr.sort { |item1, item2| item1 <=> item2 }
  end
end

books = ["Heart of Darkness", "Code Complete", "The Lorax","The Prophet", "Absalom"]

puts "A-Z: #{alphabetize(books)}"
puts "Z-A: #{alphabetize(books, true)}"
Gerry
  • 10,337
  • 3
  • 31
  • 40
  • Its not the same question. The one I had asked has completely different coding and isn't as coherent. Please take off the mark of it being a duplicate – Wilke Jones May 02 '18 at 13:40

3 Answers3

0

rev=false - this is just a default value for rev so you can call the function with or without a second parameter.

#{} - used in a string allows you to escape the string this means it executes the code inbetween the curly brackets this would be the same as "Z-A: " + alphabetize(books, true)

natschz
  • 1,007
  • 10
  • 23
  • can you simplify the (books, true) please? You're saying that it it takes the books portion so if it takes it as true does it then negate the y portion of it? How does true make it go backwards? – Wilke Jones May 02 '18 at 13:47
  • It means calling alphabetize(books) is the same as calling alphabetize(books, false). The function itself sorts the books are you pass it if rev is true it inverses the array otherwise not – natschz May 02 '18 at 14:11
0

The def foo(bar="baz", c=4) syntax is called "default arguments". It means that if there is no argument passed to the function, the argument will take the default value. For example, in foo("boo"), you've passed bar to be "boo", but since you haven't passed a value for c, it will be 4, by default.

The idea of default arguments is also present with keyword arguments. The differences between keyword arguments and default arguments are discussed in this question.

thesecretmaster
  • 1,950
  • 1
  • 27
  • 39
  • Again I just asked for it to be simple using my example. I understand its a default argument but this isn't coherent man. I appreciate the help of course but if you can simplify it that'd be appreciated – Wilke Jones May 02 '18 at 13:42
0

That is a way of setting default parameters for you function in case you don't pass one, for eg

def add(a, b, format=true)
  if format
    "a + b = #{a+b}"
  else
    a+b
  end
end

in the example above you can either call the function add with or without the format value, if you just call it as add(2,3) the value of format will be true but you can chose to set format off by doing add(2,3,false)

EDIT 1

In your example, when you do puts "A-Z: #{alphabetize(books)}" you are not passing the second argument, so in the alphabetize function, rev gets the default false value and so it goes to else block and returns you arr.sort { |item1, item2| item1 <=> item2 }

but, when you call the method with a second argument as in puts "Z-A: #{alphabetize(books, true)}" you are passing true which overwrites the default value of rev in the method so now since rev is true it goes into the if block and returns you arr.sort { |item1, item2| item2 <=> item1 }.

Hope it helps

Subash
  • 3,128
  • 6
  • 30
  • 44