Skip to content

Instantly share code, notes, and snippets.

Avatar

Aaron Gough (He/Him) aarongough

View GitHub Profile
View ternary_quandry_if_example.js
if( object.property )
{
var result = object.property;
}
else
{
var result = object.otherProperty;
}
View ternary_quandry_ternary_example.js
var result = (object.property) ? object.property : object.otherProperty;
View ternary_quandry_illustrated_example_1.js
window.firstProperty = "Hello!";
window.secondProperty = "I'm number 2!";
var result = window.firstProperty || window.secondProperty;
alert( result ); // Hello!
View ternary_quandry_illustrated_example_2.js
// window.firstProperty === undefined;
window.secondProperty = "I'm number 2!";
var result = window.firstProperty || window.secondProperty;
alert( result ); // I'm number 2!
View ternary_quandry_real_world_example.js
var viewportHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
// ^ FF, Safari, Opera ^ IE6 Strict ^ All other explorers
View ruby-assumption.rb
require 'test/unit'
class SanityTest < Test::Unit::TestCase
def test_my_sanity
complex_array = [{:blah => "hello"},{:foo => "blah"}]
copied_array = complex_array
copied_array[0][:blah] = nil
assert_not_equal copied_array, complex_array
end
View ruby_assumption2.rb
require 'test/unit'
class SanityTest < Test::Unit::TestCase
def test_my_sanity
complex_array = [{:blah => "hello"},{:foo => "blah"}]
copied_array = complex_array.clone
copied_array[0][:blah] = nil
assert_not_equal copied_array, complex_array
end
View shallow_copy.rb
require 'test/unit'
class ShallowTest < Test::Unit::TestCase
def test_standard_copy_should_be_by_reference_and_changes_should_back_propagate
simple_array = [1,"hello", 3.000]
copied_array = simple_array
copied_array[0] = nil
copied_array[1] = nil
copied_array[2] = nil
View deep_copy.rb
require 'test/unit'
class FinallySaneTest < Test::Unit::TestCase
def test_deep_copy
complex_array = [{:blah => "hello"},{:foo => "blah"}]
copied_array = Marshal.load(Marshal.dump(complex_array))
copied_array[0][:blah] = nil
assert_not_equal copied_array, complex_array
end