Cannot figure what I am doing wrong again

I am trying to follow along with this video
Ruby Programming - 16 - Multidimensional Arrays - YouTube
during the last 3 minutes or so of the video he is describing how to use .each with an array

When I try to follow along I do not get the same results
this is what I put in

string_array = [[“Fred”, “Tim”], [“Isaac”, “James”]]
string_array.each {|x| x.each {|y| puts y [0]} }

this is his results

F
T
I
J

But I get something different

F
T
I
J
[["Fred", "Tim"], ["Isaac", "James"]]

I cannot figure why it prints the names on the last line. I did not think I was asking for that

You did.

Array.html#each method Returns the array itself. (ary ) .
image

Which is printed out by Ruby Console after the puts inside the iterations. This is a normal behaviour. The Console always printing out what the last method returns.

1 Like

So, the reason it did not happen with the video was because he was not using Ruby Console?

How would I not have it return the array

Method calls in Ruby always return an object. There is no way to prevent this. So it is really a question of whether the console you are using prints this return object (the SketchUp Ruby Console window always does).

2 Likes

Ok thanks

string_array.each {|x| x.each {|y| puts y [0]} };puts
will no longer print the array as the final puts supersedes it…

But even puts returns nil, and some consoles will print that, usually with some indicator it is the returned value such as =>.

What do you mean by puts will no longer print the array. I thought it was printing it

when would puts return nil

The SketchUp console is similar to stand-alone Ruby’s irb ‘console’ in that it always shows the result of the last executed statement.

puts always returns nil, or in a normal OS console, using stand-alone Ruby, the following outputs NilClass:

ruby -e "puts (puts '').class"

Always

1 Like

OK, I have another problem
Now I am trying to follow along with this video
Ruby Programming - 20 - How to Write a Sort Method - Search (bing.com)

def sortedlist (array,  reverse  =  false)
if 
	reverse == false
	array.sort {|a,b| a <=> b}
else
	array.sort {|a,b| b <=> a}
end
end
disney_movies = ["Lion King", "Little Mermaid", "Lady and the Tramp", "Finding Nemo", "Toy Story", "Tangled"]
puts "In proper order: #{sortedlist (disney_movies)}"
puts "In backwards sort: #{sortedlist (disney_movies, true)}"

I cannot get the last line to work
When I leave off the last line it works, so it seems to be the last line that is giving me problems

I get this error

Error: #<SyntaxError: <main>:15: syntax error, unexpected ',', expecting ')'
puts "In backwards sort: #{sortedlist (disney_movies, true)}"

                                                     ^
<main>:15: unterminated string meets end of file>
SketchUp:1:in `eval'

from what the video said, this last line should put the list of disney movies in reverse order

I have gotten this type of error before when I forgot to put the " at the end of the string, but I have it here

def sortedlist(array, reverse = false)
  if reverse == false
    array.sort {|a,b| a <=> b}
  else
    array.sort {|a,b| b <=> a}
  end
end
disney_movies = ["Lion King", "Little Mermaid", "Lady and the Tramp", "Finding Nemo", "Toy Story", "Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"

Even better like this:

def sortedlist(array, reverse = false)
  if reverse
    array.sort{|a,b| b <=> a}
  else
    array.sort{|a,b| a <=> b}
  end
end
disney_movies = ["Lion King", "Little Mermaid", "Lady and the Tramp", "Finding Nemo", "Toy Story", "Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"

Even more better to read in a forum: :wink:

def sortedlist(array, reverse = false)
  if reverse
    array.sort {|a,b| b <=> a}
  else
    array.sort {|a,b| a <=> b}
  end
end
disney_movies = ["Lion King", 
                 "Little Mermaid",
                 "Lady and the Tramp",
                 "Finding Nemo", 
                 "Toy Story", 
                 "Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"
2 Likes

why did it work alright with the spaces until I added a last line?

Because the interpreter was “looking past” your poor coding syntax.

But then when you added the 2nd argument to the method call, this broke the interpreter’s ability to correctly parse the statement, hence the SyntaxError.

At one time there used to be warnings spit to STDOUT when a space occurred between a method name and the opening ( of it’s parameter list. So, the lesson is don’t put these unneeded spaces between a method name and the parameter list.

The other lesson is you need to learn how to read the error messages. It told you exactly where the error was, line number and the "^" character was pointing directly at the spot of the error.

1 Like

In addition to Dan’s comment, this may also be helpful:

Ruby Style Guide

I thought the ^ was pointing to the space before the word true. I tried leaving that space out but it did not do any good. So, yes I need to learn to read the errors.

Where can I find out about reading errors

I have been looking that over. Sometimes it confuses me worse.

The confusion will be there for a while until you reach a certain level of knowledge. You have to be patient and "fanatical "… :wink:

1 Like