Skip to content

Instantly share code, notes, and snippets.

@joellusky
Created July 23, 2014 14:08
Show Gist options
  • Save joellusky/ece8ddcfc9eb9e9fca09 to your computer and use it in GitHub Desktop.
Save joellusky/ece8ddcfc9eb9e9fca09 to your computer and use it in GitHub Desktop.
puts"Question\n Let’s start with an easy one. Write the expression 1+1 in two different, but equivalent ways."
puts"Answer\n 1+(1),1.0+(1)"
puts"Question\n Assume someone buys a product from your website for $33.50. You don’t trust float money. Can you think of something you can do to help you keep track of that value?"
puts"Answer\n33.50*100.to_i"
puts"Question \nMost things in Ruby are “introspectable”, meaning you can find out what something is and what it can do. Introspection helps you learn the language. Even a list of methods is introspectable. For example, the list of methods has methods. Output the list of methods available on a list of methods. There should be a sort method in that list. What does it do?"
puts"Answer \nThe sort method puts whatever you are sorting in alphabetical order."
puts"Question \n Type a number large enough such that calling the “class” method returns Bignum rather than Fixnum. How many digits long was it?"
puts"Answer\nTwenty digits"
puts"Question\nIf you round -1.5, is the answer -1 or -2?"
puts"Answer\n -2"
puts"Question\nWhich is “greater”, “A” or “a”?"
puts"Answer\n A"
puts"Question\nWhat does the “next” method do to a String? What happens when you use that method on “z” and “Z”?"
puts"Answer\nIt will increment the right most character in the string."
puts"Question\nThe String concat method is short for “concatenation”. What other String method is an even shorter version of concat?"
puts"Answer\n<<"
puts"Question\n What’s the difference between a String’s “length” and a String’s “size”?"
puts"Answer\n There is no difference"
puts"Question\nSometimes I type too__many___spaces____between_____words. According to the Ruby String documentation, what one line of code can replace all my double and triple-spaces with a single-spaces? (When reading this question, replace the _ with a space.)"
puts"Answer\n 'hello how are you.squeeze' "
puts"Question\n1 - 1.0/3 produces the wrong answer because it uses Floats. But Ruby’s standard library contains something called a “rational” that can express “⅓” correctly. Can you rewrite the expression 1-⅓ so that it returns the correct result? Hint: Start with the String “1/3”."
puts"Answer\n1-1/3.to_r"
puts"Question\nWhat happens when I try to convert the letter A into a Fixnum?"
puts"Answer\nIt returns a result of zero"
puts"Question\nUse a String and escape characters to print a Christmas tree. Make some of your own String art as well."
puts"Answer\nprint' x\n xxx\n xxxxx\n xxxxxxx\n xxxxxxxxx\n'"
puts"Question\nHow many spaces long is the tab escape character? Show us how you figured it out."
puts"Answer\n8 spaces long. After entering print'\tjoel' and counting the spaces it was tabbed over."
puts"Question\nHow many alerts can you print in a single line before Ruby starts to ignore you for being too annoying?"
puts"Answer\n5"
puts"Question\nWhat happens when I try the following Ruby expression? 1 + nil What do you think the error means?"
puts"Answer\nIt says nil can't be coerced into a Fixnum. Which i think means nil can not be converted into a integer."
puts"Question\nWhat’s the difference between these two lines of code? [1,2,3].push(1,2,3) [1,2,3].push([1,2,3])"
puts"Answer\nThe first line of code pushes 1,2,3 into the array creating one array that looks like this [1, 2, 3, 1, 2, 3] The second line of code pushes the [1, 2, 3] array into the first array creating an array within an array that looks like [1, 2, 3, [1, 2, 3]]"
puts"Question\nUsing a combination of Array’s join method and String’s split method, write a line of code that converts [1,2,3] into ['1', '2', '3']."
puts"Answer\n[1,2,3].join(",").split(",")"
puts "Question\n How many methods does a String have that a Symbol doesn’t?"
puts"Answer\n 88"
puts "Question\nHow many methods does a Symbol have that a String doesn’t?"
puts"Answer\n2"
puts"Question\nBased on your understanding of how Symbols work, what do you think the Array Symbol.all_symbols represents? How would you add a Symbol to this Array?"
puts"Answer\n It returns an array of all the symbols currently in rubys symbol table.. If you want to add a symbol to an array use the .push method"
puts"Question\nWrite this Hash in another way. {a: 1, b: 2}"
puts"Answer\n { a => 1, b=>2 }"
puts"Question\nCreate a Hash that represents you: hair color, eye color, gender, etc. Create another Hash representing your (ideal?) significant other. Figure out how to combine those two Hashes into a single Hash representing your (imaginary?) child. How would you write that expression to guarantee your child inherits your traits?"
puts"Answer\nme = {haircolor: 'blond', eyecolor: 'blue', gender: 'male' } other = {haircolor: 'black', eyecolor: 'green', gender: 'female'} puts other.merge(me)"
puts"Question\nWrite a 1-line Ruby expression to convert this Array: [[1,2,3], [:a, :b, :c]] into this array [1, 2, 3, :a, :b, :c]"
puts"Answer\n[[1,2,3], [:a, :b, :c]].flatten"
puts"Qestion\nUse a truth tables to prove De Morgan’s Law. Show that “not (A or B)” equals “not A and not B”."
puts"Answer\n"#answer
puts"Question\nIn a single boolean expression I want to check if an Array contains a last item. If it does, I want to output it. Otherwise I want to output the first item. But this code isn’t working. Can you explain why? [1,2,nil].last or [1,2,nil].first Expected result: nil Actual result: 1"
puts"Answer\nBecause nil is falsey, and something falsey is not considered an item. It outputs the first item."
puts"Question\nWhat’s the value of these boolean expressions? a || b && c a && (b || c) a && !(b || c) (a && b) || (d && c) Calculate the result for a = true, b = false, c = true d = true"
puts"Answer\nTrue False True False"
puts"Question\nCreate a hello_world.rb program. Run it from SublimeText and run it from Terminal. Just let us know when you're done."
puts"Answer\nDone"
puts"Quetion\nThis line of code isn’t working for me. Can you fix it? puts {a: 1, b: 2}"
puts"Answer\nputs hash1 = {a: 1, b: 2}"
puts"Question\nWhat’s the difference between the output out these two lines of code and why? puts [1,2,3] p [1,2,3]"
puts"Answer\nputs [1, 2, 3] will display each number on a separate line whereas p [1, 2, 3,] will display exactly what you see, [1, 2, 3]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment