Home > Software engineering >  Can my editor alert me to undefined variables in ruby?
Can my editor alert me to undefined variables in ruby?

Time:07-28

I'm familiar with python and needing to take on ruby development at my current job. In setting up my editor I've noticed something that bites me far more often than I like:

My editor doesn't alert me to undefined variables in ruby

From what I can gather, while Pylint will alert me to this in python, rubocop does not do the same for ruby.

What I haven't been able to determine: Is there any possible way to fill this gap?

For reference, I use neovim with CoC and Ale. CoC is for completion, and runs the LSP. I'm happy to throw another tool into the mix if needed.

Below is some contrived example code that, if fixed, is expected print:

omg
wtf
bbq
foo
bar

Python:

# pylint: disable=missing-function-docstring,disallowed-name,missing-module-docstring
def omg():
    output = "omg"
    print(output)
    return output


def wtf():
    output = "wtf"
    print(output)
    return output


def bbq():
    output = "bbq"
    print(outtputt)
    return output


def foo():
    output = "foo"
    print(outtputt)


if __name__ == "__main__":
    omg()
    wtf()
    bbq()
    foo()
    print("bar")

Ruby:

# frozen_string_literal: true

def omg
  output = 'omg'
  puts output
  output
end

def wtf
  output = 'wtf'
  puts output
  output
end

def bbq
  output = 'bbq'
  puts outtputt
  output
end

def foo
  output = 'foo'
  puts outtputt
end

if __FILE__ == $PROGRAM_NAME
  omg
  wtf
  bbq
  foo
  puts 'bar'
end

Running vanilla pylint against the above python will give me:

python_var_error.py:16:10: E0602: Undefined variable 'outtputt' (undefined-variable)
python_var_error.py:22:10: E0602: Undefined variable 'outtputt' (undefined-variable)
python_var_error.py:21:4: W0612: Unused variable 'output' (unused-variable)

Whereas running vanilla rubocop against the above ruby will only give me:

ruby_var_error.rb:22:3: W: Lint/UselessAssignment: Useless assignment to variable - output. Did you mean outtputt?
  output = 'foo'

What tool can I use (if any) to alert me to the error on line 17 in the above ruby?

Everything I've found so far (example) seems to be roughly "ruby can't do it. You're SOL."

CodePudding user response:

What tool can I use (if any) to alert me to the error on line 17 in the above ruby?

There is no (static) tool you can use to alert you to an error on line 17, because there is no (static) error on line 17. Line 17 is perfectly valid Ruby code, which calls a method named outtputt and passes the result as an argument to the method call to puts.

Since there is no (static) error, there cannot possibly be a (static) tool that shows the error.

Obviously, if the method is not defined, that will be a runtime error, and that will be caught by your tests.

  •  Tags:  
  • ruby
  • Related