Ruby String variable returning true for .is_a?(Array)

Jon Heckman

I have a method that will either get an array or a string

For the method to work correctly, I need to convert that string to an array. Since it will sometimes get string, I wanted to have it check if the variable is an array, and if not convert it to an array. So I did the following:

unless variablename.is_a?(Array)
  variablename = variablename.lines.to_a
end

The second line of this fails and I get a ruby error that 'lines' is not available to an array object.

I also tried .kind_of? with same results

I got my answer but I also wanted to clear up what exactly I was asking. I am testing to see if variablename is an array. For some reason when variablename is an array, it still runs and than the second line fails with the following error: undefined method `lines' for #Array:0x000000021382b8 (NoMethodError)

7stud
def do_stuff(x)
  x = x.lines.to_a if x.is_a? String
  x
end 

data = [
  "hello\nworld",
  [1, 2, 3]
]

data.each do |item|
  p do_stuff item
end

Now, with unless:

def do_stuff(x)
  unless x.is_a?(Array)
    x = x.lines.to_a
  end
  x
end 

data = [
  "hello\nworld",
  [1, 2, 3],
  ['a', 'b']
]

data.each do |item|
  p do_stuff item
end

--output:--
["hello\n", "world"]
[1, 2, 3]
["a", "b"]

But it makes more sense to check for a String object before calling a String method on the object than checking for not an Array.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why is my Swift string variable returning nil?

분류에서Dev

Returning string is not successful

분류에서Dev

Function returning pointer to string

분류에서Dev

PHP OOP checking if a class is returning true or false?

분류에서Dev

PHP OOP checking if a class is returning true or false?

분류에서Dev

Function always returning true no matter input

분류에서Dev

ElasticSearch Ruby returning results without ElasticSearch running

분류에서Dev

Ruby comparing values in hashes and returning the same structure

분류에서Dev

Simple Ruby variable mutability

분류에서Dev

NSArray Returning String Instead of Dictionary

분류에서Dev

Determine if expression is true for all array elements in Ruby

분류에서Dev

Ruby (Rails 4.0.2): (1 == true) => false?

분류에서Dev

Ruby Scalable String Match

분류에서Dev

Ruby on Rails String Concatenation

분류에서Dev

Is checking if a variable is equal to a value and then returning that value redundant?

분류에서Dev

BitmapFactory.decodeResource is returning 0 for outHeight & outWidth with inJustDecodeBounds = true

분류에서Dev

c# List.Contain(List) not returning true for containing list

분류에서Dev

filter function ng-repeat returning true but 0 results are displayed

분류에서Dev

variable is returning two values, how to store them in different variable?

분류에서Dev

Returning string from BlackBerry java application

분류에서Dev

Bad formatted expr returning empty string in Tcl

분류에서Dev

Returning array in Javascript function after splitting string

분류에서Dev

ruby method to convert a hash to a string

분류에서Dev

Remove trailing commas in ruby string

분류에서Dev

ruby extract string between two string

분류에서Dev

Variable as an exponent in a string (Octave)

분류에서Dev

Calling a variable with a string

분류에서Dev

Variable and string as array key

분류에서Dev

replace string with variable

Related 관련 기사

  1. 1

    Why is my Swift string variable returning nil?

  2. 2

    Returning string is not successful

  3. 3

    Function returning pointer to string

  4. 4

    PHP OOP checking if a class is returning true or false?

  5. 5

    PHP OOP checking if a class is returning true or false?

  6. 6

    Function always returning true no matter input

  7. 7

    ElasticSearch Ruby returning results without ElasticSearch running

  8. 8

    Ruby comparing values in hashes and returning the same structure

  9. 9

    Simple Ruby variable mutability

  10. 10

    NSArray Returning String Instead of Dictionary

  11. 11

    Determine if expression is true for all array elements in Ruby

  12. 12

    Ruby (Rails 4.0.2): (1 == true) => false?

  13. 13

    Ruby Scalable String Match

  14. 14

    Ruby on Rails String Concatenation

  15. 15

    Is checking if a variable is equal to a value and then returning that value redundant?

  16. 16

    BitmapFactory.decodeResource is returning 0 for outHeight & outWidth with inJustDecodeBounds = true

  17. 17

    c# List.Contain(List) not returning true for containing list

  18. 18

    filter function ng-repeat returning true but 0 results are displayed

  19. 19

    variable is returning two values, how to store them in different variable?

  20. 20

    Returning string from BlackBerry java application

  21. 21

    Bad formatted expr returning empty string in Tcl

  22. 22

    Returning array in Javascript function after splitting string

  23. 23

    ruby method to convert a hash to a string

  24. 24

    Remove trailing commas in ruby string

  25. 25

    ruby extract string between two string

  26. 26

    Variable as an exponent in a string (Octave)

  27. 27

    Calling a variable with a string

  28. 28

    Variable and string as array key

  29. 29

    replace string with variable

뜨겁다태그

보관