When Did "Assigned But Unused" Become a Warning for Ruby

When did assigned but unused become a warning for Ruby?

A quick git grep showed that this was added in this commit on June 16, 2010, then reverted on June 17, 2010 due to some problems, and was finally added on June 20, 2010.

Note: I ran git log --reverse -S "assigned but" on the Ruby git repo to find these commits.

Pry gives a bunch of warnings

Remove --warning option from .rspec file.

Parallel assignment with Ruby

The warning is about unused variables a and b, not about _. An alternative :

a, b = [1,2,3,4].values_at(0,2)

Understanding Useless assignment to variable

You're not using the sum variable for anything. The following does the same thing:

def sum(a, b)
a + b
end

Because sum is local to your get_sum method, it is not available outside of that context.

Marking an unused block variable

A * means "all remaining parameters". An _ is just another variable name, although it is a bit special. So they are different, for example the following does not make sense:

[[1, 2, 3], [4, 5, 6]].each{|*, x, *| p x}  # Syntax error

Indeed, how is Ruby supposed to know if the first star should get 0, 1 or 2 of the values (and the reverse)?

There are very few cases where you want to use a star to ignore parameters. An example would be if you only want to use the last of a variable number of parameters:

[[1], [2, 3], [4, 5, 6]].each{|*, last| p last}  # => prints 1, 3 and 6

Ruby allows you to not give a name to the "rest" of the parameters, but you can use _:

[[1], [2, 3], [4, 5, 6]].each{|*_, last| p last}  # => prints 1, 3 and 6

Typically, the number of parameters is known and your best choice is to use a _:

[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid}  # prints 2 and 5

Note that you could leave the last paramater unnamed too (like you can when using a *), although it is less obvious:

[[1, 2, 3], [4, 5, 6]].each{|_, mid, | p mid}  # prints 2 and 5

Now _ is the designated variable name to use when you don't want to use a value. It is a special variable name for two reasons:

  1. Ruby won't complain if you don't use it (if warnings are on)
  2. Ruby will allow you to repeat it in the argument list.

Example of point 1:

> ruby -w -e "def foo; x = 42; end; foo"
-e:1: warning: assigned but unused variable - x

> ruby -w -e "def foo; _ = 42; end; foo"
no warning

Example of point 2:

[[1, 2, 3], [4, 5, 6]].each{|unused, mid, unused| p mid}
# => SyntaxError: (irb):23: duplicated argument name

[[1, 2, 3], [4, 5, 6]].each{|_, mid, _| p mid}
# => prints 2 and 5

Finally, as @DigitalRoss notes, _ holds the last result in irb

Update: In Ruby 2.0, you can use any variable starting with _ to signify it is unused. This way the variable name can be more explicit about what is being ignored:

_scheme, _domain, port, _url = parse_some_url
# ... do something with port

What does Previous definition of x was here in Ruby mean?

This is really a two part warning.

bunny-2.9.2/lib/bunny/channel.rb:1580: warning: method redefined; discarding old to_s

bunny-2.9.2/lib/bunny/channel.rb:257: warning: previous definition of to_s was here

It's telling you to_s was defined for that class at channel.rb:257, then another definition of to_s for the same class was found at channel.rb:1580. It's warning you about the ambiguity and how it resolved it by using the one at channel.rb:1580.

Whats the difference between method argument with and without underscore _ in Ruby

It's just code convention to underscore a variable if you're not gonna use it later on. It has no meaning otherwise.

Use _ for unused variables.

from ... https://clearwater.readthedocs.io/en/stable/Clearwater_Ruby_Coding_Guidelines.html#naming

Also see this for further reading on style preferences.
https://github.com/rubocop/ruby-style-guide



Related Topics



Leave a reply



Submit