-2

Can anybody explain where I'm going wrong using the ruby program? I'm having trouble remembering how to be specific enough for the computer to understand without writing too long of a code any advice would be much appreciated?

def are_you_playing_banjo(name)
  name = 0
  i = 0
  while i <= word.length
    name += "Are you playing banjo? "
    char = word[i] 
  if char == "r" || char == "R"
    puts true 
  elsif 
    puts false
    end
    i += 1
    end
  return name
end
user229044
  • 232,980
  • 40
  • 330
  • 338
DCARSON99
  • 7
  • 1
  • Your first line replaces whatever gets passed in as the name. – dbugger Mar 23 '22 at 21:47
  • "Long code" is not at all a requirement for producing programs specific enough for a computer. Getting the correct results just takes precisely correct code. You've produced a lot more code here than is necessary, and the majority of it is either incorrect or not useful based on the (admittedly very vague) requirements. You would benefit more from asking questions of your teacher or looking for help on forums that are more tailored for open discussion. I can recommend https://www.rubyonrails.link/, sadly Stack Overflow's own [Ruby chat room](https://chat.stackoverflow.com/rooms/44914) is dead. – user229044 Mar 23 '22 at 23:40
  • "explain where I'm going wrong" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Mar 24 '22 at 07:06
  • https://idownvotedbecau.se/itsnotworking/ https://idownvotedbecau.se/noexceptiondetails/ – Jörg W Mittag Mar 24 '22 at 07:06

2 Answers2

2

You are doing far too much, and are making quite a lot of errors along the way. You seem to try to test every char in name for being "r" or "R", but the title says "starts with". Can you come up with a way of getting the first character of name and storing it in a variable (in your code char is used as variable, which is fine)? Your code already has a way to test if this char is an "r" or "R", which will be true or false - exactly what you want. Close with an end and you're done.

steenslag
  • 79,051
  • 16
  • 138
  • 171
0

You can use String#start_with?

# Using regexp case insensitive
"Roger".start_with?(/r/i) # => true
"roger".start_with?(/r/i) # => true
"Michael".start_with?(/r/i) # => false

# Using regexp case sensitive
"Roger".start_with?(/R/, /r/) # => true
"roger".start_with?(/R/, /r/) # => true
"Michael".start_with?(/R/, /r/) # => false

# Using string
"Roger".start_with?("R", "r") # => true
"roger".start_with?("R", "r") # => true
"Michael".start_with?("R", "r") # => false

And in method

def playing_banjo?(name)
  name.start_with?(/r/i)
end


puts  "What is your name?"
name = $stdin.gets.chomp
puts "You are playing banjo" if playing_banjo?(name)
mechnicov
  • 12,025
  • 4
  • 33
  • 56