ruby API problem

Question 1

Any string contains "xyz" If there is no period (.) Before xyz, it outputs True and If there is a period (.), Let's create a method that outputs False.

Output example:

xyz_there('abcxyz') → True xyz_there('abc.xyz') → False xyz_there('xyz.abc') → True

Tips

Use the include? method. The include? method is a method that determines whether the specified element is included in the array.

array = ["foo", "bar"]
puts array.include?("bar")
#=> true
puts array.include?("hoge")
#=> false


#### Model answer
def xyz_there(str) 
  if str.include?(".xyz")
    puts "False"
  elsif str.include?("xyz")
    puts "True"
  else
    puts "False"
  end
end



Consideration

Again, there was no commentary, but it was understandable by reading the answers! I couldn't come up with the correct answer because I couldn't master how to use the include? method. I think the way of thinking was good. (I thought the include? Method could only be used for arrays, so I wrote a description like array = ["foo", "bar"].

In addition to arrays, you can also use the following.

animal = "cat"

puts color.include?("c")
#String"cat"In"c"Is included, so true is returned

(1) Arbitrary character string with str as an argument (anything is fine) (2) If ".xyz" is included in the conditional expression, puts "False" is displayed. ③ If "xyz" is included in the subsequent elsif, return true. ④ If both are not included, return with False.

after str.include? (". Xyz") If you don't write "puts" False "", True will be called. Awaken with puts.

Recommended Posts

ruby API problem
[Ruby] Ruby API problem
ruby API problem
Ruby problem ⑦
[Ruby] FizzBuzz problem
[Ruby] FizzBuzz problem
Use Face API from Ruby
[Ruby] problem with if statement
Ruby deposit system, algorithm problem
Calendar creation problem (fun Ruby practice problem)
[Ruby] Search problem using index method
This problem is soberly difficult ... (Ruby)
Ruby learning 4
Make Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
[N + 1 problem]
[Ruby] Array
Rails API
Call API [Call]
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
Ruby setting 2
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
[Problem] Weather on consecutive holidays (Ruby edition)
Ruby Review 1
[Ruby] Module
Procedure for operating google spreadsheet with API (ruby)
[Competition Pro] Solve the knapsack problem with Ruby
Try to get redmine API key with ruby