Trying to understand how these if else statements close correctly

MildManneredMan
# 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.

Matthew

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.

edited at
0

Comments

0 comments
Login to comment

Related

How correctly close the ApplicationContext in Spring?

How to Correctly Close Resources

How to understand LockService and implement it correctly?

How to write if else statements in Brainfuck

How to understand these C++ statements when debugging

how to optimize if else statements

How to parse if /else statements with yacc

JavaScript: Trying to Understand The Else Statement of a Recursion Function Calculating Exponent Value

Why isn't my palindrome if else statements not evaluating correctly?

How to refactor these if else statements to a function

I am trying to optimize my ruby else if statements

How to understand the "println" statements in both `for` and `yield`

How to correctly close the socket?

How to correctly close OutputStream?

Trying PHP IF Else Statements for the first time

Trying to understand how to work with IFS

SQL Server if else statements not working correctly

Trying to understand how SyncLock Works

how to use Array with if else statements?

How do I get my python function to properly apply an IF-ELIF-ELSE statements correctly to all rows in my pandas dataframe?

Trying to understand how to make a UpdateOrCreate

How to correctly close unused pipes?

I don't understand how returns work with if/else statements involved -please help [java]

How to correctly close nested ZipInputStreams?

Trying to understand specific IFNA statement using multiple IF statements

How do I correctly use await inside of shorthand if else Javascript statements?

How to shorten multiple if statements (not if else)

Trying to understand If statements when trying to incorporate user Input

Trying to understand how partitions function…