Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Ruby Loops Loop Conditionals

Ingrid Bardales
Ingrid Bardales
10,616 Points

ruby loop conditionals

Is there a difference between

def get_name name = "" loop do

and

def get_name() loop do

I'm thinking there might be a subtle difference but maybe not? :)

Thank you, Ingrid

Ingrid Bardales
Ingrid Bardales
10,616 Points

Hi Jacob, my question is...would it make a difference if in the code below i left out the empty string variable name?

def get_name
  name = ""
  loop do
    print "Enter your name (minimum 2 characters, no numbers): "
    name = gets.chomp
    if name.length >= 2 && !name.index(/\d/)
      break
    else
      puts "Name must be longer than 2 characters and not contain numbers."
    end
  end
  return name
end

name = get_name()
puts "Hi #{name}."

2 Answers

Jacob Jonsson
Jacob Jonsson
21,385 Points

Well, thank you!

Yeah, the variable would not be accessible by anything outside of the loop, so you would not be able to return the variable. It would be a "local variable".

I'm sorry, didn't read through the article enough... Not entirely sure when they bring up inte course, but I'm sure they will at some point.

No problem, only happy to help!

Keep on coding, nothing fun in life is easy! ;)

Jacob Jonsson
Jacob Jonsson
21,385 Points

Yes, it would.

It all comes down to the scoop of the variable "name". If you do not declare it outside of the loop it would only be accessible inside of the loop and therefore you wont be able to return it outside of the loop. But, if you declare it outside of the loop but inside of the function it will be accessible in the whole function. (Like you do with the empty string)

Take a look at this link if you donΒ΄t understand: http://www.techotopia.com/index.php/Ruby_Variable_Scope.

And sorry if i misspelled or if my grammar is off-point. Not a native english speaker... ;)

Best regards Jacob Jonsson

Ingrid Bardales
Ingrid Bardales
10,616 Points

Hey you're English is not a problem!!! Ok i understand what you are saying..also ...would it be correct of me to say that if the empty string wasn't declared outside of the loop it wouldn't be accessible by other methods if it were needed?
I was reading the article you included and wow...I'm not there yet ...but I'm working on it! :) Also, at some point will Treehouse insist on the syntax...I would imagine in the real world developers us it....maybe...maybe not?

Once again thanx for your help Jacob!!