Skip to main content
added 48 characters in body
Source Link
the Tin Man
  • 160.9k
  • 44
  • 222
  • 308

You can use regular expressions, such as e.g. finding a type of string.:

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+$/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby caseRuby's case will use the equality operand === for this (thanks @JimDeville) also info . Additional information is available at "hereRuby Operators". This also can be done using @mmdemirbas example (without parameter) just, only this approach is cleaner for these types of cases.

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+$/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use equality operand === for this (thanks @JimDeville) also info here. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these types of cases.

You can use regular expressions, such as finding a type of string:

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+$/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby's case will use the equality operand === for this (thanks @JimDeville). Additional information is available at "Ruby Operators". This also can be done using @mmdemirbas example (without parameter), only this approach is cleaner for these types of cases.

added 5 characters in body
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+]+$/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use matchequality operand =~=== for this (thanks @JimDeville) also info here. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these types of cases.

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use match operand =~ for this. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these types of cases.

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+$/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use equality operand === for this (thanks @JimDeville) also info here. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these types of cases.

added 1 characters in body
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use match operand =~ for this. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these typetypes of cases.

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use match operand =~ for this. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these type of cases.

You can use regular expressions, such as e.g. finding type of string.

case foo when /^(true|false)$/ puts "Given string is boolean" when /^[0-9]+$/ puts "Given string is integer" when /^[0-9\.]+/ puts "Given string is float" else puts "Given string is probably string" end 

Ruby case will use match operand =~ for this. This also can be done using @mmdemirbas example (without parameter) just this approach is cleaner for these types of cases.

deleted 7 characters in body
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84
Loading
edited body
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84
Loading
added 3 characters in body
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84
Loading
Source Link
Haris Krajina
  • 15.3k
  • 12
  • 69
  • 84
Loading