change value in hash using an array of keys in ruby - arrays

i was wondering if it is possible to access an value of a hash with an array of keys as described in the post ruby use array tvalues to index nested hash of hash. My aim is not just to access this value but to change this value.
As I understood
keys.inject(hash, :fetch)
returns the value of the hash-value determined by the key-array and not it's reference. How can I accomplish to modify this value?
I know it's bad style to modify an object instead of making a copy and working with immutables but in severel cases it seems much more comfortable to do it the short way.
Thanks a lot.

Use all but the last key to get the most deeply nested Hash, then assign normally using the last key.
keys[0...-1].inject(hash, :fetch)[keys.last] = value
Ruby doesn't have references so you can't reassign the value directly. Instead you have to reassign the object pointer, which means going up one level of nesting.

Related

How do I add equal elements to an array in Firestore in Flutter?

I just figured out that in Flutter
List<int> values = [1,2,3,1,2,3];
Firestore.instance.path.updateData({"values": FieldValue.arrayUnion(values)});
results in the Firestore Array
(Firestore Array) [1,2,3]
Obviously the values are somehow mapped, removing duplicate values, although I can manually add duplicate values online in the Firebase panel (the datatype is called array respectively).
Is there a way to bypass this behaviour or is this a bug?
According to the documentation here, array union will only add the elements that are not present. Because you're using a Firestore function intended to only add new unique values, it will behave that way without being bypassed. You can certainly bypass this function, though, but not without first calling the document, retrieving the array, and appending it before updating it to Firestore.
Use
List<int> values = [1,2,3,1,2,3];
Firestore.instance.path.setData({"values": values});
It works without using FieldValue.arrayUnion

Firebase Firestore: Append/Remove items from document array

I am trying to append/remove items from an array inside of a Firestore Document but every time the entire array is replaced instead of the new value being appended. I have tried both of the following:
batch.setData(["favorites": [user.uid]], forDocument: bookRef, options: SetOptions.merge())
batch.updateData(["favorites": [user.uid]], forDocument: bookRef)
I know that instead of an array I can use an object/dictionary but that would mean storing additional data that is irrelevant (such as the key), all I need is the ID's stored inside the array. Is this something that is currently possible in Firestore?
Update elements in an array
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.
let washingtonRef = db.collection("cities").document("DC")
// Atomically add a new region to the "regions" array field.
washingtonRef.updateData([
"regions": FieldValue.arrayUnion(["greater_virginia"])
])
// Atomically remove a region from the "regions" array field.
washingtonRef.updateData([
"regions": FieldValue.arrayRemove(["east_coast"])
])
See documentation here
Actually, nowadays it is possible. With latest updates db.collection.updateData
method actually appends new item to array instead of replacing it.
Example usage can be found in Firebase documentation.
If you need to do it manually, you can use
FieldValue.arrayUnion([user.uid])
Nope. This isn't possible.
Arrays tend to be problematic in an environment like Cloud Firestore where many clients could theoretically append or remove elements from an array at the same time -- if instructions arrive in a slightly different order, you could end up with out-of-bounds errors, corrupted data, or just a really bad time. So you either need to use a dictionary (where you can specify individual keys) or replace the entire array.

Storing array data in firebase, and how ID's are generated

I have a set of objects in my firebase data that all have an array under them. When I create the initial object, I create the initial array with its first object with a line of code like this:
ref.child('items').set([{firstobject: id123}])
this seems to set the id to zero, as the first item in the array. However when I later try to push() a new item to the array with this line of code, I get a more complex id (ZwPiVMIrzbSdvfwxkts).
ref.child('items').push(someNewObject);
In your first line of code, you're calling the Firebase.set() method passing it a JavaScript array that contains a single object.
In your second line of code, you're calling the Firebase.push() method with an object.
Given that Firebase lists/collections are not the same as JavaScript arrays, you end up with a mismatch.
Unlike JavaScript arrays, Firebase's lists are architected to scale well in highly concurrent, multi-user scenarios. I'd recommend to use them instead of arrays from the start.
ref.child('items').push({firstobject: id123});
ref.child('items').push(someNewObject);
With this snippet, all your items will be stored under so-called push ids.

What is this syntax in Python

if there is a syntax like this in python
what exactly is it declaring defining?
I tried looking in in the python website did not seen anything like this
children = cell["CHILDREN"]
It is accessing the item in dictionary cell with key "CHILDREN" and assigning it to the variable children.
More useful references:
http://www.tutorialspoint.com/python/python_dictionary.htm
http://learnpythonthehardway.org/book/ex39.html
http://www.python-course.eu/dictionaries.php
That line refers to dictionaries, also known as associative arrays. The line of code in question assigns the value in the dictionary that associates to the key CHILDREN to the variable children.
Read more about this in a great related Stack Overflow question.
cell is a dictionary. A dictionary is an unordered set of key: value pairs. The element cell["CHILDREN"] is retrieved and stored in the variable children. More on dictionaries here:
https://docs.python.org/2/tutorial/datastructures.html#dictionaries
https://pythonspot.com/python-dictionaries/

How to initialize a Ref<?> field in objectify to a dummy value

I have a collection(arraylist) of Ref `s ,the objectify documentation says that I need to initialize collections for them to be persisted and hence modified in the future.....
Now , Ref points to an object but when I launch my app for the first time I dont have any objects in the data store...so whats the best way for me to initialize a dummy value......
Is my assumption that a Ref<> needs to point to a real object in the data store?
Two things:
You should just initialize an empty collection. You don't need to add anything to it. eg, field = new ArrayList<Ref<Thing>>();
It's actually not even required that you initialize the collection. It's just a good idea for reasons that will become apparent if you use the system for a while.

Resources