hash

How do I replace all the values in a hash with a new value?

Let's say I have an arbitrarily deep nested Hash h: h = { :foo => { :bar => 1 }, :baz => 10, :quux => { :swozz => {:muux => 1000}, :grimel => 200 } # ... } And let's say I have a class C defined as: class C attr_accessor :dict end How do I replace all nested values in h so that they are now C instances with the dict attri...

I can't find a method to save the values in a hash directly in rails

I'm a newbie to rails, and I have researched a whole day. I think my problem is easy. There is a table questions_tags_relation, it has two columns(no id): columns: question_id -> string tag_id -> string And I have a hash: record = {:question_id=>'111111', :tag_id=>'22222'} And there is no model 'QuestionsTags', and I don't want to...

best practice to create a generic user id

What s the best way to implement a method that creates and assings ID s to user on a asp.net application? I was thinking about using DateTime ticks and thread id I wanna make sure that there is no collision and user ids are unique. ID can be a string or long. should i use MD5 on some information that i collect from user? what would t...

Session hash does size matter?

Does size matter when choosing the right algorithm to use for a session hash. I recently read this article and it suggested using whirlpool to create a hash for session id. Whirlpool generates a 128 character hash string, is this too large? The plan is to store the session hash in a db. Is there much of a difference between maybe using...

Ruby on Rails: Submitting an array in a form

I have a model that has an attribute that is an Array. What's the proper way for me to populate that attribute from a form submission? I know having a form input with a field whose name includes brackets creates a hash from the input. Should I just be taking that and stepping through it in the controller to massage it into an array? Ex...

Reducing the depth of a Hash object

I have a hash object from an ActiveRecord. I'm going to be calling to_json on it, but I am looking to reduce the depth of the object that is being returned. Here is what my find looks like: @tags = Tag.find_all_by_type("some_type", :select => :name) The result of @tags.to_json looks like this: [{"tag": {"name": "some tag name"}}, ...

How can i use xml attributes when converting into a hash with from_xml?

I have an xml element with multiple child elements that use the same ID. The child elements are distinguishable by a "size" attribute. When I create a hash using .from_xml it lumps all these child elements 1 field. How do I parse these child elements into different fields based on the xml attribute? XML: <artist rank="48"> <name>KI...

Mysql – Detecting changes in data with a hash function over a part of table

Hi I need generate a single hash over some data in a table CREATE TABLE Table1 ( F1 INT UNSIGNED NOT NULL AUTO_INCREMENT, F2 INT default NULL, F3 Varchar(50) default NULL, .. FN INT default NULL, PRIMAR...

How can I get form_for to autopopulate fields based upon a non-model hash?

I'm building a multi-step form in rails. It's not javascript driven, so each page has its own controller action like "step1" "step2" etc. I know how to do multi-step wizards through JQuery but I don't know how to keep rails validations per page without getting into javascript, hence this way. Anyways, my model is a User object but I'm...

Ruby on Rails: Best way to fill out a model Hash or Array field?

I've got a model that has a serialized hash as a field. I'm trying to create a form that enables users to fill this out. Is there a way to use Rails form_for helper to concisely build the hash? The model is an Article, and an Article can have many authors which are stored in a serialized hash. class Article serialize :authors, Hash e...

Ruby - dynamically add property to class (at runtime)

I'm looking for a way to add properties to my already defined class at runtime, or better: class Client attr_accessor :login, :password def initialize args = {} self.login = args[:login] self.password = args[:password] end end But then, I have this hash {:swift_bic=>"XXXX", :account_name=>"XXXX", :id=>"...

Tweaking Hash#to_json

Hi guys, I've been struggling with this one the whole day. In my Rails 2.3.5 app, I had a bunch of custom code which allowed the following to happen: >> strip_hash_keys_for_json({ "a" => 1 }).to_json => "{ a: 1 }" So you see that string keys don't get quoted. It was implemented by creating a String descendant class that returned self...

What is the best way to store a spectrogram graph as a data struture that can be compared against?

I have created a process similar to Shazam that creates a Spectrogram of a given sound clip. I am trying to figure out a way in which to store this data into a database so that I can run comparisons on it. (I dont need actual code, just conceptual help on the process). For those unfamiliar with a spectrogram, its a graph of time on the ...

How to write a hashcode generator for this class?

I have a class which holds a position in three floats. I have overridden Equals like so: return Math.Abs(this.X - that.X) < TOLERANCE && Math.Abs(this.Y - that.Y) < TOLERANCE && Math.Abs(this.Z - that.Z) < TOLERANCE; This is all very well, but now I need to write a GetHashCode implementation for these vertices, and I'm stuck. ...

Strange ~~~ setting the "location.hash" cannot work with the partial page postback in IE. It works in FireFox.

Hello everyone, I got a strange problem, which I could not find a solution. In order to clarify my problem, I did a simply test page that has the same problem. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="RadicaLogic.SilverSpine.Web.Surveillance.Reports.test1" %> <!DOCTYPE html PUBLIC "-//W3C/...

How to override GetHashCode() for value types in a good way?

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? As I understand it, there's a recommendation to override the base type GetHashCode() for my own value types. I have a hard time finding good resources on the actual how to implement a good hash code scheme for different kinds of value t...

Indexing hash tables

I am just starting to learn hashtables, and so far, I know that you take the object you want to hash and put it through an hash function, then use the index it returns to get the corresponding object you want. There is something I don't understand though: What structure do you use to store the objects in so you can quickly index them wi...

Return ActiveRecord query as variable, not hash?

I am doing an ActiveRecord find on a model as such @foo = MyModel.find(:all, :select => 'year') As you can see, I only need the year column from this, so my ideal output would be ["2008", "2009", "2010"] Instead, though, I get an a hash of the models, with each one containing the year, as such: [#<MyModel year: "2008">, #<MyModel ...

Checksum of setup project files

How can I create a setup project that will check its own checksum first, before an installation? Or could you show me an example of a programm that will check it's own checksum and inform user if the programm's file probably has been changed and checksum doesn't fit... ...

Compute rank of a combination?

I want to pre-compute some values for each combination in a set of combinations. For example, when choosing 3 numbers from 0 to 12, I'll compute some value for each one: >>> for n in choose(range(13), 3): print n, foo(n) (0, 1, 2) 78 (0, 1, 3) 4 (0, 1, 4) 64 (0, 1, 5) 33 (0, 1, 6) 20 (0, 1, 7) 64 (0, 1, 8) 13 (0, 1, 9) 24 (0, 1, 10...