Calling method isn't returning string

Benjamints

I created a method to count a substring 'e' in a string passed as an argument. If there isn't a substring 'e' in the string, it should return "There is no \"e\"." I am trying to achieve this:

  • How many times 'e' is in a string.
  • If given string doesn't contain any "e", return "There is no "e"."
  • if given string is empty, return empty string.
  • if given string is nil, return nil.

This is my code:

def find_e(s)
  if !s.include?("e")
    "There is no \"e\"."
  elsif s.empty?
     ""
  else s.nil?
     nil
  end
  s.count("e").to_s
end

find_e("Bnjamin")

It skips the if statement and it still uses the method count. Why is this?

Sebastian Palma

To achieve what you want you could move your string.count to the else statement in your if, because actually you're making your method return the quantity of e passed in the count method over your string, but what happens inside the if isn't being used:

def find_e(s)
  if s.nil?
    nil
  elsif s.empty?
    ''
  elsif !s.include?("e")
    "There is no \"e\"."
  else
    s.count("e").to_s
  end
end

p find_e("Bnjamin") # => "There is no \"e\"."
p find_e("Benjamin") # => "1"
p find_e(nil) # => nil
p find_e('') # => ""

And also your validations must be in order, first check nil values, then empty values, and then the rest, if you don't then you'll get some undefined method ___ for nil:NilClass errors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a method calling an async method, but the calling method isn't async itself, though returning a Task, but how does it work?

From Dev

Why isn't my method calling?

From Dev

Why isn't my function returning the string?

From Dev

Calling a function returning a String within the main method

From Dev

Method returning True when condition isn't met

From Java

Thread isn't interrupted after even after calling interrupt method

From Java

Why isn't my object cast to interface not calling default method?

From Dev

Firebase isn't completing before calling next method

From Dev

Generate random string isn't always returning the correct length

From Dev

Why Isn't This L-System Code Returning a String?

From Dev

Why isn't this processing function returning anything related to the string?

From Dev

Async Await with method calling external resource or returning local string

From Dev

.NET how to prevent calling a method returning a string multiple times

From Dev

Why isn't phantomjs returning

From Dev

Why isn't Main returning?

From Dev

JSX isn't returning anything

From Java

Substring method in String class reaches the index it isn't supposed to

From Java

Why isn't method String.indexOf part of interface CharSequence?

From Dev

Method for checking if string is getting empty text or numbers isn't working

From Dev

Isn't string.length actually a method in JavaScript?

From Dev

Why isn't string scanner not working in my other method?

From Dev

Why isn't my alternate string replacement method working?

From Dev

Dart: client.post() method in the http.dart package is hanging and isn't returning a future

From Java

Why isn't calling a static method by way of an instance an error for the Java compiler?

From Dev

Calling static method of class loaded via different ClassLoader isn't working. Is this expected?

From Dev

Returning a view from a method by calling its method

From Dev

async await returning Task<List<T>> instead of List<T> on calling aync method

From Dev

String method returning null

From Java

Method not returning a string

Related Related

  1. 1

    Creating a method calling an async method, but the calling method isn't async itself, though returning a Task, but how does it work?

  2. 2

    Why isn't my method calling?

  3. 3

    Why isn't my function returning the string?

  4. 4

    Calling a function returning a String within the main method

  5. 5

    Method returning True when condition isn't met

  6. 6

    Thread isn't interrupted after even after calling interrupt method

  7. 7

    Why isn't my object cast to interface not calling default method?

  8. 8

    Firebase isn't completing before calling next method

  9. 9

    Generate random string isn't always returning the correct length

  10. 10

    Why Isn't This L-System Code Returning a String?

  11. 11

    Why isn't this processing function returning anything related to the string?

  12. 12

    Async Await with method calling external resource or returning local string

  13. 13

    .NET how to prevent calling a method returning a string multiple times

  14. 14

    Why isn't phantomjs returning

  15. 15

    Why isn't Main returning?

  16. 16

    JSX isn't returning anything

  17. 17

    Substring method in String class reaches the index it isn't supposed to

  18. 18

    Why isn't method String.indexOf part of interface CharSequence?

  19. 19

    Method for checking if string is getting empty text or numbers isn't working

  20. 20

    Isn't string.length actually a method in JavaScript?

  21. 21

    Why isn't string scanner not working in my other method?

  22. 22

    Why isn't my alternate string replacement method working?

  23. 23

    Dart: client.post() method in the http.dart package is hanging and isn't returning a future

  24. 24

    Why isn't calling a static method by way of an instance an error for the Java compiler?

  25. 25

    Calling static method of class loaded via different ClassLoader isn't working. Is this expected?

  26. 26

    Returning a view from a method by calling its method

  27. 27

    async await returning Task<List<T>> instead of List<T> on calling aync method

  28. 28

    String method returning null

  29. 29

    Method not returning a string

HotTag

Archive