# A name is valid is if satisfies all of the following:
# - contains at least a first name and last name, separated by spaces
# - each part of the name should be capitalized
def is_valid_name(str)
word_Split = str.split(" ")
if word_Split.length >= 2
word_Split.each do |x|
if x == x.capitalize
else
return false
end
end
return true
end
return false
end
puts is_valid_name("Kush Patel") # => true
puts is_valid_name("Daniel") # => false
puts is_valid_name("Robert Downey Jr") # => true
puts is_valid_name("ROBERT DOWNEY JR") # => false
In this code above, I understand the placement of the the first if/else statement. What I am having issues understanding is, do the 2 ends under the first return false, close the .each loop and the method?
What does the return true and return false outside the loop even do? I'm trying understand to read this code.
Since I am still new to coding, I have been writing if/else statements as: if this, do that. else, do this. or use an elsif in between.
I appreciate any help in understanding how this reads. I took a look at c++ if/else statements and they were a little easier to read. Using { } to separate them. Thanks for your patience and understanding.
The first thing I would do is take the code and fix the indentation and add newlines where helpful to increase readability. Next I've added inline comments to explain a bit of what is going on.
def is_valid_name(str)
word_Split = str.split(" ")
# at least two words?
if word_Split.length >= 2
word_Split.each do |x|
# This if block is empty and control falls through to [1]
# once all the words in the loop pass the check. This if/else
# could be replaced by `unless`
if x == x.capitalize
else
# Immediately exit the function and return false since
# a single word in the loop was not capitalized
return false
end
end
# [1] This handles the case where
# each word had a proper first letter capitalized
return true
end
# There was zero or one word, return false.
# Technically in ruby, the word `return` is optional
# for the last line in the method
return false
end
In this code above, I understand the placement of the the first if/else statement. What I am having issues understanding is, do the 2 ends under the first return false, close the .each loop and the method?
No. The two end
immediately after the first return false
closes the if/else
statement, then the loop. The two follow ends
close the first if
statement and then the method.
What does the return true and return false outside the loop even do? I'm trying understand to read this code.
The return true
handles the case all the words (at least 2 or more) all started with a capital letter. The return false
handles the case where there is only zero or one word after attempting the split.
As a former C/C++ dev, I recommend you look at Ruby conditionals as a good point to continue reading. Specifically learn about if, else, elsif, and unless
as well as trailing conditionals.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments