Skip to content

Instantly share code, notes, and snippets.

/**
* Inspired from Mootools 1.2 Element.Storage (http://blog.mootools.net/2008/1/22/Element_Storage)
*
* License:
* MIT-style license.
*
* Author:
* Sebastien Grosjean (http://zencocoon.com)
*
* Tested under FF 2, FF3, Safari 3, Opera 9.25, IE6, IE7, Camino 1.6
# In Ruby
# How to deeply fetch an Hash
# Having :
hash = {"parent" => {"child" => "value"}}
# How to get value from "parent.child" ?
# Ideal solution
hash.deep_fetch(["parent", "child"])
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
<script>
/* Alternate snippet to asynchronously embed the Google Analytics script.
* This requires the following things of the pages that will contain it:
* 1. the page only include one GA tracker (common case),
* 2. the page is not be behind SSL (http, not https),
* 3. this script block can be in the <head> or <body> elements
*/
var _gaq = [['_setAccount', 'UA-XXXXXX-X'],['_trackPageview']];
(function(d) {
<html>
<Head>
<Title> Identifiez vous </Title>
<script type="text/javascript">
//Verifie coté client que le formulaire est bien rempli
function control(form_Authentification)
{
var va_login=document.form_Authentification.login.value;
var va_password=document.form_Authentification.password.value;
## JSON output naturally ordered
# Actual result
>> { :b => "b", :a => ["a", "c", "b", { :c => "c", :a => "a", :b => "b"}], :c => "c" }.to_json
=> "{\"b\":\"b\",\"c\":\"c\",\"a\":[\"a\",\"c\",\"b\",{\"b\":\"b\",\"c\":\"c\",\"a\":\"a\"}]}"
# Desired result
>> { :b => "b", :a => ["a", "c", "b", { :c => "c", :a => "a", :b => "b"}], :c => "c" }.to_json
=> "{\"a\":[\"a\",\"c\",\"b\",{\"a\":\"a\",\"b\":\"b\",\"c\":\"c\"}],\"b\":\"b\",\"c\":\"c\"}"
>> returning(ActiveSupport::OrderedHash.new) do |map| %w(a c b).each{|n| map[n] = n } end.to_json
=> "{\"a\":\"a\",\"c\":\"c\",\"b\":\"b\"}"
>> returning(ActiveSupport::OrderedHash.new) do |map| %w(a c b).each{|n| map[n] = n } end.sort.to_json
=> "[[\"a\",\"a\"],[\"b\",\"b\"],[\"c\",\"c\"]]"
The idea is to apply a method (in this case adding 1) for all elements from a array (here Integer), this should be done deeply.
Original: [1, 2, 3, ["a", 1, 2, 3, ["a", 1, 2, 3], "a"]
Desired: [2, 3, 4, ["a", 2, 3, 4, ["a", 2, 3, 4], "a"]
function parents_ids($n) {
return($n["out_out_i_id"]);
}
function display_with_parent_id($array, $parent_id) {
foreach($array as $item) {
if ($item["out_out_i_id"] == $parent_id) {
echo $item["out_va_nom"];
var_dump(array_map("parents_ids", $array));
if (in_array($item["out_i_id"], array_map("parents_ids", $array))) {
S2.enableMultitouchSupport = true;
new S2.UI.Carousel("hcarousel", {drag: true});