Skip to content

Instantly share code, notes, and snippets.

@andyweiss1982
Created July 22, 2014 21:47
Show Gist options
  • Save andyweiss1982/9212fabd8c5df2a6f233 to your computer and use it in GitHub Desktop.
Save andyweiss1982/9212fabd8c5df2a6f233 to your computer and use it in GitHub Desktop.
IRB Portfolio
puts "RUBY SYNTAX"
puts ""
puts "Question: Ruby Typing Practice"
puts ""
puts "You should practice typing Ruby code until it feels natural."
puts "You should be able to type Ruby code as fast as you can type English."
puts "Go through each slide in the lecture and type each line of code you find into irb."
puts "Code is always written in the Courier font. Do not copy-and-paste."
puts "There are traps in the slides that will get you into trouble if you do that."
puts "When you're done, just respond to this question with 'ok'."
puts ""
puts "Answer:"
puts ""
puts "OK" #I did this wrong the first time.
puts ""
puts "Question: 1+1"
puts ""
puts "Let’s start with an easy one."
puts "Write the expression 1+1 in two different, but equivalent ways."
puts ""
puts "Answer:"
puts ""
puts "1) 1.+(1)" #This was simple enough.
puts "2) [1,1].reduce(:+)" #I learned this today when we went over the homework.
puts ""
puts "Question: Float Money"
puts ""
puts "Assume someone buys a product from your website for $33.50."
puts "You don’t trust float money."
puts "Can you think of something you can do to help you keep track of that value?"
puts "" #Something about the wording of this question totally threw me for a loop.
puts "Answer:"
puts ""
puts "You could store the value as a string."
puts "Alternatively, you could multiply it by 100 and save the total number of cents."
puts "Also, you could create an array that saves the number of dollars and cents separately."
puts "In practice, however, developers use more complex solutions to the problem of float money."
puts "" #Will we be learning more about these at some future date?
puts "Question: Methods Introspection"
puts ""
puts "Most things in Ruby are “introspectable”,"
puts "meaning you can find out what something is and what it can do."
puts "Introspection helps you learn the language."
puts "Even a list of methods is introspectable."
puts "For example, the list of methods has methods."
puts "Output the list of methods available on a list of methods." #I did this by typing methods.methods
puts "There should be a sort method in that list. What does it do?"
puts ""
puts "Answer:"
puts ""
puts "The sort method arranges items in an array."
puts "Numbers are listed in ascending order, and letters are listed alphabetically."
puts "I believe symbols come before numbers and letters, but I am not sure how they are ordered."
puts ""
puts "RUBY DATA TYPES"
puts ""
puts "Question: Practice Typing Ruby"
puts ""
puts "You know the drill. Type every line of code from the lecture into irb."
puts "They should all work. Just let us know when you're done."
puts "There's a lot of code in this lecture."
puts "You may want to go through it a few times to get used to typing all the different types of data."
puts ""
puts "Answer:"
puts ""
puts "OK" #This time I did it correctly.
puts ""
puts "Question: Fixnum Bignum"
puts ""
puts "Type a number large enough such that calling the class method returns Bignum rather than Fixnum."
puts "How many digits long was it?"
puts ""
puts "Answer:"
puts ""
puts "20 digits long" #This took some trial and error.
puts ""
puts "Question: Float Rounding"
puts ""
puts "If you round -1.5, is the answer -1 or -2?"
puts ""
puts "Answer:"
puts ""
puts "-2"
puts ""
puts "Question: String Comparisons"
puts ""
puts "Which is “greater”, “A” or “a”?"
puts ""
puts "Answer:"
puts ""
puts "“a” is greater." #counterintuitive but interesting
puts ""
puts "Question: The Next String"
puts ""
puts "What does the next method do to a String?"
puts "What happens when you use that method on “z” and “Z”?"
puts ""
puts "Answer:"
puts ""
puts "The next method returns the same string but advances the final value by one letter or number."
puts "“z”.next returns “zz” and “Z”.next returns “ZZ”"
puts ""#I wonder what happens if the string ends in some other kind of character.
puts "Question: Another String Concetenation" #typo: should read Concatenation
puts ""#This was a new word for me. I wonder if concatenate is related to the word cadena in Spanish.
puts "The String concat method is short for “concatenation”."
puts "What other String method is an even shorter version of concat?"
puts ""
puts "Answer:"
puts ""
puts "The “<<” and “+” operators appear to have the same effect as concat."
puts "" #I am not entirely sure this is the answer the questioner is looking for.
puts "Question: Length vs. Size"
puts ""
puts "What’s the difference between a String’s “length” and a String’s “size”?"
puts ""
puts "Answer:"
puts ""
puts "Calling length or size on the same string appears to return the same value in every case."
puts "The Ruby documentation says the size is just an alias for length."
puts ""#I finally learned to navigate the Ruby documentation.
puts "Question: To Many Spaces" #Too many spaces?
puts ""
puts "Sometimes I type too many spaces between words."
puts "According to the Ruby String documentation,"
puts "what one line of code can replace all my double and triple-spaces with a single-spaces?"
puts ""
puts "Answer:"
puts ""
puts "“Too many spaces”.split.join(“ ”)" #I found this on stack overflow.
puts ""
puts "Question: Rationals"
puts ""
puts "1 - 1.0/3 produces the wrong answer because it uses floats."
puts "But Ruby’s standard library contains something called a “rational” that can express “⅓” correctly."
puts "Can you rewrite the expression 1-⅓ so that it returns the correct result?"
puts ""
puts "Hint: Start with the String “1/3”."
puts ""
puts "Answer:"
puts ""
puts "1 - “1/3”.to_r" #This was a total guess and I was pleasantly surprised when it worked.
puts ""
puts "Question: String to Number"
puts ""
puts "What happens when I try to convert the letter A into a Fixnum?"
puts ""
puts "Answer:"
puts ""
puts "0"
puts ""
puts "Question: Christmas Tree"
puts ""
puts "Use a String and escape characters to print a Christmas tree."
puts ""
puts " x "
puts " xxx "
puts " xxxxx "
puts " xxxxxxx "
puts "xxxxxxxxx"
puts ""
puts "Answer:"
puts ""
p " x \n xxx \n xxxxx \n xxxxxxx \nxxxxxxxxx"
puts ""
puts "becomes:"
puts ""
puts " x \n xxx \n xxxxx \n xxxxxxx \nxxxxxxxxx"
puts ""
puts "in irb."
puts ""#This took me way too long to figure out. I should have read the slides more carefully.
puts "Here is some more string art:"
puts ""
p " x x \n\n xxxxxxx"
puts ""
puts "becomes"
puts ""
puts " x x \n\n xxxxxxx"
puts ""
puts "Question: Tab Length"
puts ""
puts "How many spaces long is the tab escape character? Show how you figured it out."
puts ""
puts "Answer:"
puts ""
puts "The tab escape character is 8 spaces long."
puts "I tried a few things but ultimately this was the most elegant:"
puts ""
p "123456789\n\t<tab ends here\n123456789"
puts ""
puts "This is how it looks in irb:"
puts ""
puts "123456789\n\t|tab ends here\n123456789"
puts ""#This was a lot of trial and error. For a while I was using \x09 for tab. Should have read the slides sooner.
puts "Question: Alert Limit"
puts ""
puts "How many alerts can you print in a single line"
puts "before Ruby starts to ignore you for being too annoying?"
puts ""
puts "Answer:"
puts ""
puts "print “alert” * 1000000000 worked but adding an extra zero broke my terminal window."
puts "I tried to find the exact number through trial and error,"
puts "but after crashing the terminal window a few times I decided I was close enough."
puts "" #My computer started acting really weird after that.
puts "Question: Nil Math"
puts ""
puts "What happens when I try the following expression?"
puts ""
puts "1 + nil"
puts ""
puts "What do you think the error means?"
puts ""
puts "Answer:"
puts ""
puts "It says: nil can't be coerced into Fixnum."
puts "I think it means that 1 and nil can't be added,"
puts "because nil isn't a number and can't be converted into one."
puts ""
puts "Question: Push or Push"
puts ""
puts "What’s the difference between these two lines of code?"
puts ""
puts "[1,2,3].push(1,2,3)"
puts "[1,2,3].push([1,2,3])"
puts ""
puts "Answer:"
puts ""
puts "The first line returns an array with six fixnums inside of it."
puts "The second line returns an array with two arrays inside of it, each one with three fixnums."
puts ""#I answered all of these by just plugging the code into irb and seeing what happens.
puts "Question: Split Join"
puts ""
puts "Using a combination of Array’s join method and String’s split method," #apostrophe
puts "write a line of code that converts [1,2,3] into [“1”, “2”, “3”]."
puts ""
puts "Answer:"
puts ""
puts "[1,2,3].join.split(“”)"
puts ""
puts "Question: String vs. Symbol"
puts ""
puts "How many methods does a String have that a Symbol doesn’t?"
puts ""
puts "Answer:"
puts ""
puts "88" #I got this like so: ("a".methods - :a.methods).count
puts ""
puts "Question: Symbol vs. String"
puts ""
puts "How many methods does a Symbol have that a String doesn’t?"
puts ""
puts "Answer:"
puts ""
puts "2" #I got this like so: (:a.methods - "a".methods).count
puts ""
puts "Question: All The Symbols"
puts ""
puts "Based on your understanding of how Symbols work,"
puts "what do you think the Array Symbol.all_symbols represents?"
puts "How would you add a Symbol to this Array?"
puts ""
puts "Answer:"
puts ""
puts "I think Symbol.all_symbols represents all symbols in the class Symbol."
puts "You can add a new symbol by inputting any text with a colon in front of it."
puts ""#I'm a little shaky on this.
puts "Question: OG Hash"
puts ""
puts "Write this Hash in another way."
puts ""
puts "{a: 1, b: 2}"
puts ""
puts "Answer:"
puts ""
puts "{:a => 1, :b =>2}"
puts ""
puts "Question: Hash Genetics"
puts ""
puts "Create a Hash that represents you: hair color, eye color, gender, etc."
puts "Create another Hash representing your (ideal?) significant other."
puts "Figure out how to combine those two Hashes into a single Hash representing your (imaginary?) child."
puts "How would you write that expression to guarantee your child inherits your traits?"
puts ""
puts "Answer:"
puts "me = {gender: :m, haircolor: :brown, eyecolor: :brownish}"
puts "mate = {gender: :f, haircolor: :brown, eyecolor: :brown}"
puts "mate.merge(me)"
puts ""#I had a little help with this. Afterwards I figured out how to do it without variables, but I like the way this looks.
puts "Question: 2D Array"
puts ""
puts "Write a 1-line Ruby expression to convert this Array:"
puts "[[1,2,3], [:a, :b, :c]]"
puts "into this array"
puts "[1, 2, 3, :a, :b, :c]"
puts ""
puts "Answer:"
puts ""
puts "[[1,2,3], [:a, :b, :c]].flatten"#I got lucky here with a combination of stack overflow and calling methods on an empty array.
puts ""
puts "INTRO TO BOOLEAN LOGIC"
puts ""
puts "Question: Use the following truth tables to prove De Morgan’s Law."
puts "Show that “not (A or B)” equals “not A and not B”."
puts ""
puts "-------------------------------------"
puts "not (A or B) | A=true | A=false |"
puts "-------------------------------------"
puts " B=true | | |"
puts "-------------------------------------"
puts " B=false | | |"
puts "-------------------------------------"
puts ""
puts "-------------------------------------"
puts "not A and not B| A=true | A=false |"
puts "-------------------------------------"
puts " B=true | | |"
puts "-------------------------------------"
puts " B=false | | |"
puts "-------------------------------------"
puts ""
puts "Answer:"
puts ""
puts "-------------------------------------"
puts "not (A or B) | A=true | A=false |"
puts "-------------------------------------"
puts " B=true | FALSE | FALSE |"
puts "-------------------------------------"
puts " B=false | FALSE | TRUE |"
puts "-------------------------------------"
puts ""
puts "-------------------------------------"
puts "not A and not B| A=true | A=false |"
puts "-------------------------------------"
puts " B=true | FALSE | FALSE |"
puts "-------------------------------------"
puts " B=false | FALSE | FALSE |"
puts "-------------------------------------"
puts ""
puts "Question: Boolean Hack"
puts ""
puts "In a single boolean expression I want to check if an Array contains a last item."
puts "If it does, I want to output it. Otherwise I want to output the first item."
puts "But this code isn’t working. Can you explain why?"
puts ""
puts "[1,2,nil].last or [1,2,nil].first"
puts ""
puts "Expected result: nil"
puts "Actual result: 1"
puts ""
puts "Answer:"
puts ""
puts "The first array does contain a last item, but that last item is nil," #I'm clear on this
puts "so the first array outputs “nil”. The second array contains the a first item, which is “1.”" #and this
puts "So effectively the code can be reduced to “nil or 1.”" #and this
puts "Nil is falsey but 1 is truthy, so Ruby will take this and return 1." #but not fully sure why this is
puts ""
puts "Question: Boolean Logic"
puts ""
puts "What’s the value of these boolean expressions?"
puts ""
puts "a || b && c"
puts "a && (b || c)"
puts "a && !(b || c)"
puts "(a && b) || (d && c)"
puts ""
puts "Calculate the result for a = true, b = false, c = true d = true"
puts ""
puts "Answer:"
puts ""
puts "1) True"
puts "2) True"
puts "3) False"
puts "4) True"
puts ""#I wrote these expressions out on paper to solve them.
puts "RUBY PROGRAMS"
puts ""
puts "Question: So Sublime"
puts ""
puts "Create a hello_world.rb program. Run it from SublimeText and run it from Terminal."
puts "Just let us know when you're done."
puts ""
puts "Answer:"
puts ""
puts "OK"
puts ""
puts "Question: Puts Hash"
puts ""
puts "This line of code isn’t working for me. Can you fix it?"
puts ""
puts "puts {a: 1, b: 2}"
puts ""
puts "Answer:"
puts ""
puts "puts ({a: 1, b: 2})" #I'm not sure if this is what the questioner is looking for but it seems to work.
puts ""
puts "Question: Puts Array"
puts ""
puts "What’s the difference between the output out these two lines of code and why?"
puts ""
puts "puts [1,2,3]"
puts "p [1,2,3]"
puts ""
puts "Answer:"
puts ""
puts "Puts returns each integer on a new line."
puts "P returns a one-line array with each number inside of it."
puts "" #I just tried both and described what I saw.
puts "Question: IRB Portfolio"
puts ""
puts "It’s time to start working on your portfolio. Create a file named wyncode.rb in SublimeText."
puts "Go through all the homework you’ve been assigned thus far, questions and answers,"
puts "and enter them into that file. Use puts to display information that would be useful to a user,"
puts "and use comments to record information that would be useful to you."
puts "When I run your wyncode.rb, I should see all your questions and answers in one long sheet."
puts "When I view the source code for wyncode.rb, I should see comments that would be helpful"
puts "to someone just learning to code (e.g. you, 2 days ago)."
puts "Create a gist (https://gist.github.com/) for your wyncode.rb file and post the link."
puts ""
puts "Answer:"
puts ""
puts "If you are reading this, I must have done it correctly." #pray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment