Jasdeep Singh
Jasdeep Singh

Reputation: 3326

ruby - using include method in a case statement

I'm using something like this:

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

Unfortunately, this returns nil even if the string some_string is present in the variable referer.

Here's what i tried in Ruby Console:

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

Any inputs will be appreciated.

Upvotes: 22

Views: 15866

Answers (4)

fl00r
fl00r

Reputation: 83680

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

Upvotes: 0

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

jasdeep = "RAILS"

case
  when jasdeep.include?("AI")
    puts "Hello"
end

Upvotes: 3

Aj Gu
Aj Gu

Reputation: 1439

Try this

jasdeep = "RAILS"
case jasdeep
when /IL/
  puts "Hello"
end

Upvotes: 40

勿绮语
勿绮语

Reputation: 9330

That nil is the returned value from the puts statement, not from .include?. Try to run the following two statements separately from the console and observe the returned value:

jasdeep.include? "AI"

puts "hello"

Upvotes: 0

Related Questions