-1

Here is my function:

def run_test_non_compilation(short_name,error_label)

@short_name = short_name

$error_label =  error_label

# more coding here
 end

What I want is as follows:

  • If I execute

run_test_non_compilation('MDL_ConfigRS001',"HKI_load_cnf")

It should give the same result as if i called my function using

run_test_non_compilation('MDL_ConfigRS001',"hki_load_cnf")

or

run_test_non_compilation('MDL_ConfigRS001',"hki_LOAD_cnf")

etc...

The variable error_label must be case insensitive when using it inside the function.

Bijoy Thangaraj
  • 5,434
  • 4
  • 43
  • 70
Mourad
  • 474
  • 4
  • 10
  • Why do you use `$error_label` and `$log_file_name` global variables? If I understand context correctly, they should be local variables. General principle in Ruby is to avoid using global variables if you can. – Marek Lipka Jan 26 '15 at 14:18
  • 1
    @MarekLipka: yeah, I foresee some hard debugging sessions if this goes on :) – Sergio Tulentsev Jan 26 '15 at 14:20
  • I actually cannot. I already did, but you got at least 2 down votes, so mine is bringing you UP to -1. – chad_ Jun 25 '15 at 10:52

2 Answers2

2

I see that you check if your error_label is present in your file. To make it case-insensitive, just use match and pass into it Regexp ignoring case:

if line.match(/#{error_label}/i)
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • i tried your suggestion but it does not work, because in fact downcase it convert all upercases to lowercases and this not what i want i want the parameter $error_label to be case insensitive eg : if i pass the argument HKI_load_cnf it will be same as hki_Load_cnf or hki_LOAD_Cnf or Hki_LOad_cNF you see what i want – Mourad Jan 26 '15 at 14:36
  • @Mourad I think you need to be a little bit expressive. What happens after you tried it? How exactly does your code look like now? – Marek Lipka Jan 26 '15 at 14:38
  • what i want i want the parameter $error_label to be case insensitive eg : if i pass the argument HKI_load_cnf it will be same as hki_Load_cnf or hki_LOAD_Cnf or Hki_LOad_cNF you see what i want – Mourad Jan 26 '15 at 14:40
  • @Mourad it was quite hard to understand what you mean, but I think I managed to do it. I edited my answer. On a side note: I guess you should learn some Ruby basics before writing more complicated applications. – Marek Lipka Jan 26 '15 at 14:47
1

Case insensitive comparison can be done with String#casecmp :

if($error_label.casecmp(some_string) == 0) {
  # do stuff
end

To be clear, there is no way to make a string variable become case insensitive in Ruby. You can compare a string using some_string.casecmp(some_other_string). This will return 0 if the strings are the same, ignoring case.

Second, you don't really want to put that in a global variable. Are you maybe a PHP developer? It's common when moving from PHP to Ruby to make global ($) vars when you intend to use instance vars (@).

Regardless, knowing what you intend to do with the string would help a great deal. If you expect to take any case of the string, and find some configuration file on a case sensitive file system, you'd have to enumerate the configuration names then do something like config_array.select {|a| $error_label.casecmp(a) == 0 } where config_array is something like ['HKI_load_cnf'] and that is the true capitalization. I don't know though, I'm really grasping to understand your problem.

chad_
  • 3,749
  • 2
  • 22
  • 22