Last active
August 29, 2015 14:02
-
-
Save ryanholm/7bd2447e87543c797985 to your computer and use it in GitHub Desktop.
BLOC Loops
This file contains 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
each_with_index {|obj, i| block } → enum click to toggle source | |
Calls block with two arguments, the item and its index, for each item in enum. | |
hash = Hash.new | |
%w(cat dog wombat).each_with_index {|item, index| | |
hash[item] = index | |
} | |
hash #=> {"cat"=>0, "wombat"=>2, "dog"=>1} |
This file contains 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
#CP1 | |
class Array | |
def sum_numbers | |
sum_numbers=0 | |
self.each do |num| | |
sum_numbers+=num | |
end | |
sum_numbers | |
end | |
#RSpec | |
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 | |
#CP1 2 | |
def add_index | |
self.each_with_index do |item, index| | |
addIndexToElements << "#{index} is #{item}" | |
end | |
addIndexToElements | |
end | |
end | |
#RSpec | |
describe "Array" do | |
describe "add_index" do | |
it "adds index to numbers" do | |
[1, 2, 3].add_index.should eq(["0 is 1", "1 is 2", "2 is 3"]) | |
end | |
it "adds index to strings" do | |
new_array = ["0 is apple", "1 is banana", "2 is orange"] | |
["apple", "banana", "orange"].add_index.should eq(new_array) | |
end | |
end | |
end | |
#CP1 3 | |
class String | |
def camel_case | |
camelCaseArray = [] | |
self.split.each_with_index do |item, index| | |
if index == 0 | |
camelCaseArray << item.downcase | |
else | |
camelCaseArray << item.capitalize | |
end | |
end | |
camelCaseArray.join | |
end | |
end | |
#RSpec | |
describe "String" do | |
describe "camel_case" do | |
it "leaves first word lowercase" do | |
"test".camel_case.should eq("test") | |
end | |
it "should lowercase first letter if it isn't" do | |
"Test".camel_case.should eq("test") | |
end | |
it "should combine words using camel case" do | |
"This is a test".camel_case.should eq("thisIsATest") | |
end | |
it "should downcase words with capitals" do | |
"MUST dOWNCASE words".camel_case.should eq("mustDowncaseWords") | |
end | |
end | |
end | |
#CP2 | |
class String | |
def title_case | |
fixTitle = [] | |
self.split.each_with_index do |item, index| | |
downcase = item.downcase | |
if ((downcase == "and" || downcase == "a" || downcase == "the" || downcase == "of") && index != 0) | |
fixTitle << downcase | |
else | |
fixTitle << item.capitalize | |
end | |
end | |
fixTitle.join(" ") | |
end | |
end | |
#RSpec | |
describe "String" do | |
describe "Title case" do | |
it "capitalizes the first letter of each word" do | |
"the great gatsby".title_case.should eq("The Great Gatsby") | |
end | |
it "works for words with mixed cases" do | |
"liTTle reD Riding hOOD".title_case.should eq("Little Red Riding Hood") | |
end | |
it "ignores articles" do | |
"The lord of the rings".title_case.should eq("The Lord of the Rings") | |
"the sword and the stone".title_case.should eq("The Sword and the Stone") | |
"the portrait of a lady".title_case.should eq("The Portrait of a Lady") | |
end | |
it "works for strings with all uppercase characters" do | |
"THE SWORD AND THE STONE".title_case.should eq("The Sword and the Stone") | |
end | |
end | |
end | |
This file contains 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
#Notes that are a good reference for working with irb (else) | |
1.9.3-p392 :003 > 1+2 | |
=> 3 | |
1.9.3-p392 :004 > class Foo | |
1.9.3-p392 :005?> def foo | |
1.9.3-p392 :006?> print 1 | |
1.9.3-p392 :007?> end | |
1.9.3-p392 :008?> end | |
=> nil | |
1.9.3-p392 :009 > a = [1,2,3,4] | |
=> [1, 2, 3, 4] | |
1.9.3-p392 :010 > a.each do |num| | |
1.9.3-p392 :011 > p "The num argument is assigned to: #{num}" | |
1.9.3-p392 :012?> end | |
"The num argument is assigned to: 1" | |
"The num argument is assigned to: 2" | |
"The num argument is assigned to: 3" | |
"The num argument is assigned to: 4" | |
=> [1, 2, 3, 4] | |
1.9.3-p392 :013 > a = ["job", "getting", "better", "millionaire"] | |
=> ["job", "getting", "better", "millionaire"] | |
1.9.3-p392 :014 > a.each do |text| | |
1.9.3-p392 :015 > p "The text argument is assigned to: #{text}" | |
1.9.3-p392 :016?> end | |
"The text argument is assigned to: job" | |
"The text argument is assigned to: getting" | |
"The text argument is assigned to: better" | |
"The text argument is assigned to: millionaire" | |
=> ["job", "getting", "better", "millionaire"] | |
1.9.3-p392 :017 > | |
#Notes that are a good reference for working with irb (while) | |
server:~ holmertraining$ irb | |
1.9.3p392 :001 > names = ["Steve", "Bill", "Sergey"] | |
=> ["Steve", "Bill", "Sergey"] | |
1.9.3p392 :002 > index = 0 | |
=> 0 | |
1.9.3p392 :003 > while names[index] | |
1.9.3p392 :004?> p names[index] | |
1.9.3p392 :005?> index += 1 | |
1.9.3p392 :006?> end | |
"Steve" | |
"Bill" | |
"Sergey" | |
=> nil | |
1.9.3p392 :007 > |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment