Ruby on Rails Monday, July 2, 2018


On Tue, Jul 3, 2018 at 7:03 AM, Phil Edelbrock <edelbrp@gmail.com> wrote:
 >> a = MyItem.table_name
 => "my_items"
 >> b = a
 => "my_items"
 >> c = b
 => "my_items"
 >> d = c
 => "my_items"

​var references object.​
at this point, all vars a,b,c,d points to MyItem.table_name
 
 >> d << ", other_items"
 => "my_items, other_items"     <---- Same result!  Looks right.
 >> c
 => "my_items, other_items"
 >> MyTable.table_name
 => "my_items, other_items"  <--- Oh noes, the result was inherited all the way down to the table_name of the model!

Now the MyItem model is borked until the app is restarted.

The malformed 'from' clause was a bit of a red herring and that was easily resolved, btw. (A 'join(', ')' was skipped because the construction of the from-clause was a string not an array any more in our code.)

The '<<' method appears to work the same as the String method ".concat(something)" .  It also makes all other variables inherit the same value.


it does more, ​just be careful since it flattens.


(from ruby core)
=== Implementation from Array
------------------------------------------------------------------------
  ary.concat(other_ary1, other_ary2,...)   -> ary

------------------------------------------------------------------------

Appends the elements of +other_ary+s to self.

  [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
  [ "a" ].concat( ["b"], ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
  [ "a" ].concat #=> [ "a" ]

 
Another possible solution is to use '.dup', for what it is worth:


​indeed

 >> d = c.dup
 => "my_items"
 >> d << ", other_items"
 => "my_items, other_items"
 >> c
 => "my_items"

I hope this helps somebody if they run into a similar problem!  (I don't claim this is a Ruby bug or such, just perhaps not very well documented feature.)


(from ruby core)
=== Implementation from Array
------------------------------------------------------------------------
  ary << obj            -> ary

------------------------------------------------------------------------

Append---Pushes the given object on to the end of this array. This
expression returns the array itself, so several appends may be chained
together.

  [ 1, 2 ] << "c" << "d" << [ 3, 4 ]
          #=>  [ 1, 2, "c", "d", [ 3, 4 ] ]

fwiw, you're function/method seems to do a lot of things. maybe best to create a unit test for it, no?
 

Phil

​kind regards​
,
--botp

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAwHHQjCS8zL46U7ygrViqUj%2BrW1cRdW7ejptVVWHSXH8TrTLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment