Skip to content

Instantly share code, notes, and snippets.

View beezeebly's full-sized avatar

Beez beezeebly

View GitHub Profile
@beezeebly
beezeebly / gist:8174642
Created December 29, 2013 20:45
Ruby: using each with a Hash
book = { "author" => "J.K. Rowling",
"title" => "Harry Potter and the Philosopher's Stone",
"isbn" => "9788478888566"}
book.each {|key, value| puts "#{key} is #{value}"}
# => author is J.K. Rowling
# => title is Harry Potter and the Philosopher's Stone
# => isbn is 9788478888566
@beezeebly
beezeebly / gist:8174512
Created December 29, 2013 20:34
Ruby Hash example
empty_hash = Hash.new # => {}
default_hash = Hash.new("Harry Potter and the Philosopher's Stone")
empty_hash["book"] # => nil
default_hash["book"] # => "Harry Potter and the Philosopher's Stone"
@beezeebly
beezeebly / gist:8174279
Created December 29, 2013 20:12
Ruby hash access example
book = { "author" => "J.K. Rowling",
"title" => "Harry Potter and the Philosopher's Stone",
"isbn" => "9788478888566"}
book["author"] # => "J.K. Rowling"
@beezeebly
beezeebly / gist:8174234
Created December 29, 2013 20:06
Ruby hash symbol example
book = { author: "J.K. Rowling",
title: "Harry Potter and the Philosopher's Stone",
isbn: "9788478888566"}
@beezeebly
beezeebly / gist:8174203
Created December 29, 2013 20:04
Ruby hash example
book = { "author" => "J.K. Rowling",
"title" => "Harry Potter and the Philosopher's Stone",
"isbn" => "9788478888566"}
@beezeebly
beezeebly / gist:8174088
Created December 29, 2013 19:51
Ruby array map method example
evens = [2, 4, 6, 8]
evens.map { |even| even*2 }
# => [4, 8, 12, 16]
@beezeebly
beezeebly / gist:8173981
Last active January 1, 2016 16:59
Ruby pop and push explained
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
authors.push("Neil Gaiman")
authors # => ["Arthur Conan Doyle",
# "J.K. Rowling",
# "J.R.R. Tolkien",
# "Hanif Kureishi",
# "Neil Gaiman"]
authors.pop()
authors # => ["Arthur Conan Doyle",
# "J.K. Rowling",
@beezeebly
beezeebly / gist:8173928
Created December 29, 2013 19:36
Ruby: using a negative index to retrieve an element in an array
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
authors[-4] # => "Arthur Conan Doyle"
authors[-3] # => "J.K. Rowling"
authors[-2] # => "J.R.R. Tolkien"
authors[-1] # => "Hanif Kureishi"
@beezeebly
beezeebly / gist:8173910
Created December 29, 2013 19:34
Ruby array element retrieval
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
authors[0] # => "Arthur Conan Doyle"
authors[1] # => "J.K. Rowling"
authors[2] # => "J.R.R. Tolkien"
authors[3] # => "Hanif Kureishi"
@beezeebly
beezeebly / gist:8173838
Created December 29, 2013 19:27
Ruby literal constructed array
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]