开发者

Initialize two variables on same line

开发者 https://www.devze.com 2023-03-05 17:21 出处:网络
I\'m having trouble finding an authoritative example or discussion of this concept. If I have 2 variables in my Ruby method that are numbers, and I need to initialize them to zero. They will be used a

I'm having trouble finding an authoritative example or discussion of this concept. If I have 2 variables in my Ruby method that are numbers, and I need to initialize them to zero. They will be used as counters. Is this OK or safe? It works in my tests.

Instead开发者_如何学编程 of this:

foo = 0
bar = 0

You can do this?

foo = bar = 0

It seems like a nice way to save on lines and still be expressive. Or, is this a bad practice? I realize I would be sacrificing readability a little. But, in a small Ruby method (<10 lines), that might not be a valid concern.


This works, but you should know that it's only safe for your situation because you're using the variables for numbers, which are immutable in Ruby. If you tried the same thing with a string, for example, you could end up with some behavior you didn't expect:

ruby-1.9.2-p180 :001 > foo = bar = "string"
 => "string" 
ruby-1.9.2-p180 :002 > foo.upcase!
 => "STRING" 
ruby-1.9.2-p180 :003 > bar
 => "STRING"


It is fine practice to initialize two variables on the same line as long as the value being assigned as short and easy to follow. You can even assign two different values using the array syntax.

foo = bar = 123
foo #=> 123
bar #=> 123

foo, bar = 1, 2
foo #=> 1
bar #=> 2


In many languages, the latter assigns the same object to both variables, which can be trouble if the object is mutable. Better to do this:

foo, bar = 0, 0


This is weird.

Ignacio Vazquez-Abrams answer is true for initializing variable with different values with:
a, b = 123, 456

But, I think your concern is on doing the following:
a = b = "hello"

But WAIT! The above line allocates same memory address for variables a and b i.e.
a.object_id and b.object_id are equal. So any changes on a would persist on b.

I don't like it, but a workaround could be.
x = "a very long string or something you don't want to type multiple times"
a, b, c = x.clone, x.clone, x.clone

Oh I wish there was an elegant way of doing this.


It's safe and it works.

Depends completely on your coding standard whether something like this is frowned upon or not.

Use it and leave for later refactoring when the time comes.


A possible solution, to assign the same value to different variables, is to call Array.new explicitly, like so:

a, b, c = Array.new(3, 0)

This wouldn't make a lot of sense for three variables, but could be more useful when assigning many more at once.

0

精彩评论

暂无评论...
验证码 换一张
取 消