0
puts "Enter the String for checking Character Length"

@character = gets.chomp

puts "Are you want to exclude spaces from the enterede string?(yes/no)"

@options = gets.chomp

if @options == "yes" or @options == "Yes"
    @character = @character.gsub(" ", "")
    puts "Are you want to change the case of a string?(upper/lower)"
    @case_choice = gets.chomp
  if @case_choice == "upper"
    @character = @character.upcase
    puts "@result ==== #{@character.inspect}"
        @character = @character.length
        puts "@character === #{@character.inspect}"
  elsif @case_choice == "lower"
    @character = @character.downcase
    puts "@result ==== #{@character.inspect}"
        @character = @character.length
        puts "@character === #{@character.inspect}"
  else
        puts "Please enter valid options"
  end
elsif @options == "no" or @options == "No"
    if @case_choice == "upper"
    @character = @character.upcase
    puts "@result ==== #{@character.inspect}"
        @character = @character.length
        puts "@character === #{@character.inspect}"
  elsif @case_choice == "lower"
    @character = @character.downcase
    puts "@result ==== #{@character.inspect}"
        @character = @character.length
        puts "@character === #{@character.inspect}"
  else
        puts "Please enter valid options"
  end
else
    puts "Please enter valid options"
end

If the user enters the wrong option?then it goes to the inital stage. For example if the user enters 'jhgh' instead of 'Yes' means it should be go to inital stage. How can i done this?

  • 2
    Hint: You'd need a loop for a `while`, until you get a `'Yes'` as input. – Surya Jun 19 '17 at 13:01
  • You're not using using instance variables correctly. I'd recommend reading about variable scope in Ruby. Also, you're using `or` instead of `||`. – the Tin Man Jun 19 '17 at 17:14

2 Answers2

1

Use a while loop. Something like this:

while !['yes', 'no'].include?(@options.downcase) do
  puts "Please enter valid options"
  @options = gets.chomp
end

if @options.downcase == 'yes'
  # ...
elsif @options.downcase == 'no'
  # You could also just write `else` here
  # Since we already know that this will always be true
  # ...
end

The same pattern could be used for the @case_choice input. Additionally, there is no need to repeat this logic twice (within each branch of the initial if statement); you can simply place it after the first if statement to avoid duplication.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • In Ruby `loop` with a `break` is preferred over `while` loops. – the Tin Man Jun 19 '17 at 17:14
  • @theTinMan Huh? According to who? Your approach is equally valid, but TIMTOWTDI and I'm not aware of any styleguides voicing a strong preference between the two. – Tom Lord Jun 19 '17 at 20:08
  • According to who? According to Matz, who wrote the language. https://stackoverflow.com/a/10713963/128421 and http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6745 – the Tin Man Jul 03 '17 at 19:57
-1

Meditate on this:

loop do

  puts 'Enter the string to check character length'
  characters = gets.chomp

  loop do
    puts 'Do you want to exclude spaces from the entered string? (y/n)'
    spaces_allowed = gets.chomp.downcase
    if spaces_allowed == 'y' || spaces_allowed == 'n'
      characters = characters.delete(' ') if spaces_allowed == 'n'
      break
    end
    puts 'Please enter valid spaces_allowed'
  end

  loop do
    puts 'Change the case of a string to upper or lower case? (u/l)'
    case_choice = gets.chomp.downcase

    if case_choice == 'u' || case_choice == 'l'
      characters = case case_choice
                  when 'u'
                    characters.upcase!
                  when 'l'
                    characters.downcase!
                  end
      break
    end
    puts 'Please enter valid case.'
  end

  puts "result: #{characters}"
  puts "characters: #{characters.length}"

  puts 'Run again? (y/n)'
  break if gets.chomp.downcase == 'n'
end

This is closer to code written the "Ruby way". There are many other ways to write certain aspects of it, but for clarity and simplicity I chose to do it this way.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303