Skip to content

Instantly share code, notes, and snippets.

View fresh5447's full-sized avatar

Douglas Walter fresh5447

View GitHub Profile
@fresh5447
fresh5447 / gist:8979283
Created February 13, 2014 17:07
Methods
Best Method for this argument?
p add(1,2)
p add(4,5)
p add(7,7)
------------------------------------
Why doesn't this work
```def add (a,b)
@fresh5447
fresh5447 / Loops - Title Case
Created February 19, 2014 21:31
Loops - Title Case
class String
def title_case
array_of_words = self.downcase.split
list_of_articles = ["the", "of", "and", "a"]
array_of_words.each do |word|
unless list_of_articles.include? word
word.capitalize!
end
end
array_of_words.first.capitalize!
def hash_to_array(h = {})
a = []
h.each do |word, index|
a << "#{word} is #{index}" #build on the array
end
# h.each {|word, index| a << "#{word} is #{index}" }
a #return the array
end
# {name: "Bob"}
class User
attr_accessor :name, :email, :bio, :age, :sex
def initialize(config = {})
@name = config[:name] || "n/a"
@email = config[:email] || "n/a"
@bio = config[:bio] || "n/a"
@age = config[:age] || "n/a"
@sex = config[:sex] || "n/a"
end
def add_item(item, list)
list << item unless list.include?(item)
list
end
def remove_item(item, list)
list.delete(item)
list
end
def reverse_plus_one(a)
a << a.first
a.reverse
end
p reverse_plus_one([1,2,3,4])
def pluses_everywhere(a)
a.join("+")
@fresh5447
fresh5447 / Loop's: Each Method
Created February 19, 2014 21:37
Specs: describe "Array" do describe "sum_numbers" do it "sums consecutive numbers" do [1,2,3].sum_numbers.should eq(6) end it "sums random numbers" do [5,23,4].sum_numbers.should eq(32) end end end describe "String" do describe "camel_case" do it "leaves first word lowercase" do "test".camel_case.should eq("test") end it "should lowercase first …
class String
def camel_case
# "this is A TEST"
array_of_words = self.split
# ["this","is","A","TEST"]
array_of_words.each do |foo|
# "this"
foo.capitalize!
# "This"
@fresh5447
fresh5447 / Hash Methods
Created February 19, 2014 22:41
describe "merge_us" do it "merges two hashes that are unique" do h1 = { name: "Computer", cost: "$1,000" } h2 = { first_name: "Bob", age: 34 } new_hash = { name: "Computer", cost: "$1,000", first_name: "Bob", age: 34 } merge_us(h1, h2).should eq(new_hash) end it "merges two hashes that are have some things in common" do h1 = { name: "Computer", …
def merge_us(h1 = {}, h2 = {})
h1.merge(h2)
end
h1 = { name: "Computer", cost: "$1,000" }
h2 = { first_name: "Bob", age: 34 }
merge_us(h1, h2)
def my_keys(h = {})
class Array
def new_map
a = []
self.each do |item|
a << yield(item)
end
a
end
def new_map!(&block)
self.replace(self.new_map(&block))
def add_two(array)
array.map { |item|"#{item} + 2 = #{item + 2}"}
end
add_two([1,2])
#["1 + 2 = 3", "2 + 2 = 4"]