Out of scope variables?


#1

https://www.codecademy.com/en/courses/learn-ruby/lessons/ordering-your-library/exercises/what-youll-be-building-4?action=lesson_resume&link_content_target=interstitial_undefined

The example code works fine but I have a question and it might just end up being the most stupidest question ever

Anyway

In this code below, the method doesn’t return anything then how is arr.sort printing when we call the method alphabetize?
doesn’t arr.sort just die soon as we come out of the method?
How is method printing the sorted array arr[] and not books[]?

Any clarification would be much appreciated


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, Absalom!"]

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


https://www.codecademy.com/en/courses/learn-ruby/lessons/ordering-your-library/exercises/what-youll-be-building-4?action=lesson_resume&link_content_target=interstitial_undefined


#2

Actually it does return a sorted array.
The last evaluated expression in the function is implicitly returned.
Also note that arr itself is not modified, because arr.sort doesn’t modify (arr.sort! does)

def f
    5
end

puts f  # outputs 5

#3

Thank you for your input
Kinda makes sense and not at the same time.

So regardless of what, method will always return something or return whatever the last statement in last evaluation?

Tried adding two strings after reverse to test it and you’re right but it’s still boggles me as to why. Guess I like the good old days of returning a value manually lol

def alphabetize (arr, rev=false)

  i="Ascending"
  j="Descending"
  arr.sort!
	if rev
    arr.reverse!
    j
  else
	  arr
    i
  end
    
  
end

array= ["harry","adam","chris","mohammed","john"]

puts alphabetize(array)

puts ""
puts alphabetize(array,true)



#4

Last expression* I don’t think ruby has statements at all. (But I barely know any ruby, maybe there are exceptions) and 99% (made up statistic, duh) of the time when some blog or what have you talks about statements in ruby, they’re wrong.

Also, I think using the non-bang versions is generally preferable unless you specifically want to modify the original value (in particular you don’t want multiple parts of the code to consider themselves the owner of the same value, changing it in conflicting ways, by not modifying them you remove the possibility for all bugs of that kind)

And yes, all expressions (including method calls) evaluate to a value (even if it’s nil)

Consider:

# this if-expression evaluates to 5, and so does the first assignment
c = b = (a = if true; 5 else 6 end) + 1
puts a  # 5
puts b  # 6
d = puts c  # 6
puts d  # nil (not shown, string conversion is '' (empty))

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.