Skip to content

Instantly share code, notes, and snippets.

View samuraijane's full-sized avatar

Matthew Day samuraijane

View GitHub Profile
@samuraijane
samuraijane / key-value-array.php
Created April 23, 2015 21:03
Key/value pairs in PHP are common so when you need to iterate through an array and return only the value, this is how you do it.
<?php
$array = array(
"Events",
"Webinars",
"Cloud",
"Collaboration",
"Innovation",
"Tips"
)
@samuraijane
samuraijane / hash_from_two_arrays.rb
Created April 17, 2015 17:15
Sometimes you need to create a new hash that blends two arrays, where the content in first array become the keys and the content in the second array become the values in the new hash. You can also assign that same value to each key if necessary.
# OPTION 1
# array_a is an array that holds what will become the keys in the new hash.
# array_b is an array that holds what will become the values in the new hash.
# Create a new empty hash.
my_hash = {}
# Send array_a and array_b into the new hash.
array_a.each_with_index { |key, value| my_hash[key] = array_b[value] }
# This works well for arrays that have the same amount of content so that things
# line up. I haven't researched what happens with one array has more content
# than the other.
@samuraijane
samuraijane / hash_key_options.rb
Created April 17, 2015 16:27
To pull just the keys out of a hash, you have a few options but they are not interchangeable.
# This works in IRB but I could not get it to output in a browser. The command
# 'puts' will not output to a browser but with other methods, doing something
# like '{ |x| x}' usually works but not here.
hash.each_key { |x| puts x }
#This works to output to a browser. The output is an array.
hash.key
@samuraijane
samuraijane / nested_map_variable.rb
Created March 31, 2015 21:54
When dealing with nested maps that create an array you need to access, where you define the variable that holds the array is important. See the differences here.
#Example 1
$myvariable= imgarray.map do |layouts|
layouts[ 'layouts' ].map do |layout_name|
layout_name[ 'layout_name' ]
end
end
#Placing the variable at the beginning of the loop returns an array within an array:
#[["annie", "billy", "charlie", "danny", "eddy", "fergie", "granny", "henry", "jenny", "kelsey"]]
#Example 2
@samuraijane
samuraijane / block_or_line.rb
Last active August 29, 2015 14:18
You can write using a block or write the code on one line when nesting maps. This code shows you how to do both.
#Code with block
$myvariable= imgarray.map do |layouts|
layouts[ 'layouts' ].map do |layout_name|
layout_name['layout_name']
end
end
#Code on one line
$myvariable= (imgarray.map{ |layouts| (layouts[ 'layouts' ]).map{ |layout_name| (layout_name[ 'layout_name' ]) }} )
@samuraijane
samuraijane / to_s versus join.rb
Last active August 29, 2015 14:17
to_s and join are both used to convert to string but they are not exactly the same (research pending).
<%= 'filexyz ' + (filexyz.class).to_s %> #>filexyz String
<%= (['filexyz'] + [filexyz.class]).join(" ") %> #>filexyz String
@samuraijane
samuraijane / Nested Maps.rb
Created March 25, 2015 06:35
The proper way to nest maps
<%= hobbiestostring = '[' + (friends.map{ |friend| (friend[ 'children' ]).map{ |nino| nino[ 'name'] }}).join(", ") + ']' %>
@samuraijane
samuraijane / For versus Each
Created March 25, 2015 03:01
Each is considered better form than a for loop. Although they achieve the same results, the variable in the for loop is retained after it executes (which can make debugging more difficult) while the variable in each only lives inside the block.
<%
for item in ids
puts item
end
%>
<%
ids.each do |item|
puts item
end
@samuraijane
samuraijane / Define variable within `each`
Created March 25, 2015 00:36
An `each` statement is basically a loop in which you can define a variable. However, you can only use that variable within the statement itself. If you need that variable outside of the statement, use `do`.
#Each statements do not allow their variables to be used outside of the statement.
<%= friends.each{|friend| friend['children']} %>
#To do this use `do`.
<% friends.each do
|friend| @uno = friend['name']
end %>
<%= "#{@uno}" %>
@samuraijane
samuraijane / Animate display: none
Last active August 29, 2015 14:17
Since CSS cannot animate a <div> with display: none to another <div> with display: block, you need to use jquery.
// remap jQuery to $
(function($){
// Can also be used with $(document).ready()
$(window).load(function() {
$('#navs-menu').click(function () {
$('.nav-menu').slideToggle(300);