ruby - использование метода include в регистре - PullRequest
19 голосов
/ 22 мая 2011

Я использую что-то вроде этого:

case referer
      when (referer.include? "some_string")
        redirect_link = edit_product_path
      when (referer.include? "some_other_string")
        redirect_link = other_product_path
end

К сожалению, это возвращает ноль, даже если строка some_string присутствует в переменной referer.

Вот что япопробовал в консоли Ruby:

ruby-1.8.7-p334 :006 > jasdeep = "RAILS"
ruby-1.8.7-p334 :026 > case jasdeep
ruby-1.8.7-p334 :027?>   when (jasdeep.include? "AI")
ruby-1.8.7-p334 :028?>   puts "Hello"
ruby-1.8.7-p334 :029?> end
=> nil

Любые входные данные будут оценены.

Ответы [ 4 ]

34 голосов
/ 22 мая 2011

Попробуйте это

jasdeep = "RAILS"
case jasdeep
when /IL/
  puts "Hello"
end
3 голосов
/ 22 мая 2011
jasdeep = "RAILS"

case
  when jasdeep.include?("AI")
    puts "Hello"
end
1 голос
/ 16 декабря 2011
case true
when referer.include? "some_string"
  redirect_link = edit_product_path
when referer.include? "some_other_string"
  redirect_link = other_product_path
end
0 голосов
/ 22 мая 2011

Это nil - это возвращаемое значение из оператора put, а не из .include ?. Попробуйте запустить следующие два оператора отдельно от консоли и просмотрите возвращаемое значение:

jasdeep.include? "AI"

puts "hello"
...