clone

Both dup and clone return different objects, but modifying them alters the original object

元气小坏坏 提交于 2019-12-05 05:52:57
I have an array of values that I use as a reference for order when I'm printing out hash values. I'd like to modify the array so that the array values are "prettier". I figured I'd just dup or clone the array, change the values and the original object would remain unchanaged. However (in irb)... @arr = ['stuff', 'things'] a = @arr.clone b = @arr.dup So, at the very least, a and @arr are different objects: a.object_id == @arr.object_id => false But now it gets strange a[0].capitalize! a => ['Stuff', 'things'] @arr => ['Stuff', 'things'] ##<-what? b => ['Stuff', 'things']## <-what??? ok... so

Does cloning provide a performance improvement over constructors/factory methods?

人走茶凉 提交于 2019-12-05 05:38:29
I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example: public class Foo { private SomeObject obj; // SomeObject implements Cloneable public Foo() { obj = new SomeObject(); obj.setField1("abc"); // these fields will have the same value every time obj.setField2("def"); } public void doStuff() { SomeObject newObj = obj.clone(); // clone it instead of using a factory method // do stuff with newObj } } The usual caveats about premature optimization notwithstanding,

How to get the tag changeset after you clone or pull to a tag using mercurial?

橙三吉。 提交于 2019-12-05 05:31:04
As the definite guide aptly points out (search for "Tags and cloning"): When you run hg clone -r foo to clone a repository as of tag foo , the new clone will not contain any revision newer than the one the tag refers to, including the revision where the tag was created. The result is that you'll get exactly the right subset of the project's history in the new repository, but not the tag you might have expected. It means hg tags in your new clone does NOT show the foo tag. Same thing happens if you had cloned before foo tag was added, and you do hg pull -r foo . (Digression: tag is about the

Algorithm to clone a graph

大兔子大兔子 提交于 2019-12-05 03:43:10
Algorithm to clone a tree is quite easy, we can do pre-order traversal for that. Is there an efficient algorithm to clone a graph? I tried a similar approach, and concluded we need to maintain a hash-map of nodes already added in the new graph, else there will be duplication of nodes, since one node can have many parents. It suffices to do a depth first search and copy each node as it's visited. As you say, you need some way of mapping nodes in the original graph to corresponding copies so that copies of cycle and cross edges can be connected correctly. This map also suffices to remember nodes

Differences between angular.copy() and JSON.parse(JSON.stringify())?

放肆的年华 提交于 2019-12-05 03:22:23
Can someone explain the differences between angular.copy() and JSON.parse(JSON.stringify())? Are there any? What you will recommend to use? Is angular.fromJson(angular.toJson()) the same as JSON.parse(JSON.stringify())? Just to mention, I've read How do I correctly clone a JavaScript object? for JSON.parse(JSON.stringify()) and angular.copy() reference for angular.copy(). What JSON.parse(JSON.stringify()) won't copy: functions any object that has a special representation, like Date (it will get copied but not as Date ) properties with the value undefined angular.fromJson(angular.toJson()) is

Underscore's Cloning of Mongoose Objects and Deleting Properties Not Working?

女生的网名这么多〃 提交于 2019-12-05 03:16:02
I'm using Mongoose and I want to remove the _id property from my Mongoose instance before I send the JSON response to the client. Example: var ui = _.clone(userInvite); delete ui["_id"]; console.log(JSON.stringify(ui)); //still has "_id" property, why? The previous didn't work. However, if I do: var ui = JSON.parse(JSON.stringify(userInvite)); //poor man's clone delete ui["_id"]; console.log(JSON.stringify(ui)); //"_id" is gone! it works! I don't understand why calling delete on a cloned object using Underscore doesn't work, but if I do the hacky JSON.string/JSON.parse, it works. Any thoughts

How do I “fork” a Stream in .NET?

喜欢而已 提交于 2019-12-05 02:59:53
As discussed before , when a BinaryReader or BinaryWriter gets closed, its underlying Stream get closed as well (aargh). Consider this situation: a routine R is passed a MemoryStream, say M ; I would like to write some stuff to M and then pass it to another routine for more processing (not necessarily writing). For convenience, I'd like to wrap M in a BinaryWriter to do my writing. After writing, I'm done with the BinaryWriter but not with M . void R(MemoryStream M) { using (B = new BinaryWriter(M)) { // write some stuff using B } S(M); // now pass M to another routine for further processing }

When (deep) Cloning, use String.Copy or str1 = str2?

折月煮酒 提交于 2019-12-05 02:36:44
问题 When (deep) Cloning a custom object, should I use clone.str1 = String.Copy(obj.str1) or clone.str1 = obj.str1 ? I'd prefer the latter - shorter and quicker, but is it "safe"? I'd point to this thread, but, however, not sure what to use. 回答1: Yes - the latter choice (simple assignment) is safe for strings (in managed code), as this code illustrates: string s1 = "Initial Value"; string s2 = s1; Console.WriteLine("String1: " + s1); Console.WriteLine("String2: " + s2); s1 = "New Value"; Console

How to deep clone the state and roll back in Vuex?

限于喜欢 提交于 2019-12-05 01:40:29
问题 In Vuex I would like to take a snapshot / clone of an object property in the tree, modify it, and later possibly roll back to the former snapshot. Background: In an application the user can try out certain changes before applying them. When applying the changes, they should effect the main vuex tree. The user can also click «cancel» to discard the changes and go back to the former state. Example: state: { tryout: {}, animals: [ dogs: [ { breed: 'poodle' }, { breed: 'dachshund' }, ] ] } User

When converting from SVN to Git, how do I put the revision number in the commit message?

拥有回忆 提交于 2019-12-05 00:57:54
问题 We will be converting our repository from Subversion to Git, but would like to be able to retain the SVN revision number as comments in the bug tracker regularly reference it. We will be using git svn clone and the process described in John Albin's blog. Is there a way to include the revision number in the commit message? I'd prefer to do it during the clone, but a post-processing step would be acceptable. Yes, I know about git svn find-rev , but that requires the SVN repository stick around