Skip to content

Instantly share code, notes, and snippets.

@huezoaa
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huezoaa/c17b607a23bfcf1bbf08 to your computer and use it in GitHub Desktop.
Save huezoaa/c17b607a23bfcf1bbf08 to your computer and use it in GitHub Desktop.
wyncode.rb
# My favorite questions from the Wyncode Ruby Units
#
#UNIT: Ruby syntax.
puts "Ruby Syntax: Float Money"
puts <<FLOATMONEY
------------
| Question |
------------
Someone buys a product from your website for $33.50.
You don’t trust float money because it can introduce fractional pennies.
And you don’t want to use any rounding or truncating strategies.
In words, describe something you could do with a Float to help you keep track of this money value?
------------
|My Answer:|
------------
To avoid a possible rounding error, I would call the rationalize (.rationalize) method on my Float objects
to convert them into rational objects, which are exact.
The operations I perform on my new rational objects would not suffer from rounding problems.
After all operations are performed, I would convert the results back to Float with the .to_f method for viewing.
FLOATMONEY
#Unit: RUBY DATA TYPES: PUSH IT GOOD
puts "Ruby Data Types: Push it Good"
puts <<PUSHIT
------------
| Question |
------------
What’s the difference between these two Ruby expressions?
[1,2,3].push(1,2,3)
[1,2,3].push([1,2,3])
------------
|My Answer:|
------------
[1,2,3].push(1,2,3) will append the values integers 1,2, and 3 to the existing array. Result: [1,2,3,1,2,3]
[1,2,3].push([1,2,3]) will append the array [1,2,3] to the existing array. Result: [1,2,3,[1,2,3]]
PUSHIT
#Unit: RUBY DATA TYPES: SPLIT JOIN
puts "Ruby Data Types: Split Join"
puts <<SPLITJOIN
------------
| Question |
------------
Using a combination of Array’s join method and String’s split method,
write a single Ruby expression that converts [1,2,3] into ["1", "2", "3"].
------------
|My Answer:|
------------
[1,2,3].join(“ ”).split
SPLITJOIN
#Unit: RUBY DATA TYPES: STRING VS. SYMBOL
puts "Ruby Data Types: Strings vs. Symbol"
puts <<STRING
------------
| Question |
------------
How many methods does the String "a" have that the Symbol :a doesn’t?
Calculate the difference with Ruby.
------------
|My Answer:|
------------
(“a”.methods - :a.methods).count
=> 91
It is NOT the same result if the command is:
("a".methods.count)-(:a.methods.count)
=> 89
STRING
#Unit: RUBY DATA TYPES: SYMBOL VS. STRING
puts "Ruby Data Types: Symbols vs. Strings"
puts <<SYMBOL
------------
| Question |
------------
How many methods does the Symbol :a have that the String "a" doesn’t?
Calculate the difference with Ruby.
------------
|My Answer:|
------------
:a.methods - “a”.methods
=> [:id2name, :to_proc]
Answer: 2
SYMBOL
#Unit: RUBY DATA TYPES: BE RATIONAL
puts "Ruby Data Types: Be Rational"
puts <<RATIONAL
------------
| Question |
------------
1 - 1.0/3 produces the wrong answer (0.6666666666666667) because it outputs a Float.
But Ruby’s standard library contains something called a Rational that can express “⅓” correctly.
Can you rewrite the Ruby expression so that it returns the correct result?
------------
|My Answer:|
------------
((1.rationalize) - (1.0.rationalize)/(3.rationalize))
=> (2/3)
Additional correct answers:
1 - 1.0.to_r/3 => (2/3)
Or
1 - "1.0/3".to_r => (2/3)
Or
1 - Rational("1/3") => (2/3)
1 - Rational(1,3) => (2/3)
1 - 1.rationalize/3 => (2/3)
RATIONAL
#Unit: RUBY DATA TYPES: STRINGY CHRISTMAS TREE
puts "Ruby Data Types: Stringy Christmas Tree"
puts <<CHRISTMAS
------------
| Question |
------------
print a single String containing newline characters that looks like a Christmas tree.
    x
   xxx
  xxxxx
xxxxxxx
xxxxxxxxx
Make some of your own String art as well.
------------
|My Answer:|
------------
print “ x\n xxx\n xxxxx\n xxxxxxx\nxxxxxxxxx\n”
print “xxxxx\nxxxx\nxxx\nxx\nx\nx\nxx\nxxx\nxxxx\nxxxxx\n”
CHRISTMAS
#Below is the code actually running!
print " x\n xxx\n xxxxx\n xxxxxxx\nxxxxxxxxx\n"
print "xxxxx\nxxxx\nxxx\nxx\nx\nx\nxx\nxxx\nxxxx\nxxxxx\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment