This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_item(item, list) | |
list << item unless list.include?(item) | |
list | |
end | |
def remove_item(item, list) | |
list.delete(item) | |
list | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("+") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_two(array) | |
array.map { |item|"#{item} + 2 = #{item + 2}"} | |
end | |
add_two([1,2]) | |
#["1 + 2 = 3", "2 + 2 = 4"] |
OlderNewer