Skip to content

Instantly share code, notes, and snippets.

@froilainhaeckse
Forked from chrisberkhout/ruby_foundations.rb
Created November 7, 2016 15:53
Show Gist options
  • Select an option

  • Save froilainhaeckse/a7fc374926e022d6e424e9bc9a92ae6c to your computer and use it in GitHub Desktop.

Select an option

Save froilainhaeckse/a7fc374926e022d6e424e9bc9a92ae6c to your computer and use it in GitHub Desktop.

Revisions

  1. @chrisberkhout chrisberkhout created this gist Jul 7, 2016.
    221 changes: 221 additions & 0 deletions ruby_foundations.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,221 @@
    # PURPOSE ######################################################################

    # This exercise aims not to practice or build skills, but to quickly identify
    # topics and details of the Ruby language for further review. It intentionally
    # ranges from easy to things that are probably hard or unknown.

    # INSTRUCTIONS #################################################################

    # There are 70 small tasks. Aim to spend one hour on them. If you don't know the
    # answer just move on. If you are unsure of the syntax just guess. Answer
    # inline, under each task description. The code doesn't have to execute. When a
    # task extends a previous answer just copy and paste your old answer and
    # continue. When the task doesn't specify certain details just make up whatever
    # you want. Whenever the task asks you to print, you should probably use the
    # puts method.


    # TOPIC: NUMBERS ###############################################################

    # Task 1: divide 3 by 2 to get 1.5

    # Task 2: cast 1.000000001 into the integer 1

    # Task 3: what is (1.999999999).to_int


    # TOPIC: SYMBOLS ###############################################################

    # Task 1: make symbols named chris, %age

    # Task 2: make a symbol from the variable some_string


    # TOPIC: STRINGS, ESCAPING AND INTERPOLATION ###################################

    # Task 1: make a string

    # Task 2: make a string with a " and a ' and a newline in it

    # Task 3: with string interpolation make a string including variables myname and yourname

    # Task 4: get the first letter of a string s


    # TOPIC: VARIABLES #############################################################

    # Task 1: assign a string to a variable


    # TOPIC: NIL ###################################################################

    # Task 1: What is the value of a variable you haven't set yet?

    # Task 2: How can you tell if a variable is set to nil?


    # TOPIC: ARRAYS ################################################################

    # Task 1: assign an array of the numbers 1 to 3 to a variable, then append 4 to it

    # Task 2: use the previous array to make a new one with 5 to 10 appended to it

    # Task 3: get the first item from an array

    # Task 4: get the last item from an array

    # Task 5: get the 3rd to 5th items out of an array

    # Task 6: get the second last and last items from an array

    # Task 7: make an array that contains 3 arrays, each of 3 elements

    # Task 8: from the string s = "one two three", generate an array ["one", "two", "three"]

    # Task 9: from the array ["one", "two", "three"], generate a string "one;two;three"


    # TOPIC: HASHES ################################################################

    # Task 1: make a new empty hash

    # Task 2: make hash mapping keys a, b, c to values 1, 2, 3

    # Task 3: for hash h, set key d to value 4

    # Task 4: retrieve from the hash the value associated with key b

    # Task 5: get all the keys from a hash

    # Task 6: get all the values from a hash

    # Task 7: make a nested hash of scores broken down first by name, then by best/worst


    # TOPIC: EXPRESSIONS AND OPERATOR PRECEDENCE ###################################

    # Task 1: What is the result of `3 - 1 * 2`?

    # Task 2: Make the above expression equivalent to `2 * 2` without removing anything


    # TOPIC: CONDITIONALS: IF, UNLESS, BOOLEAN VALUES ##############################

    # Task 1: use if/else to print 'small' if x is 1 or less, 'medium' if it is 2..5 and 'large' otherwise

    # Task 2: use unless after the division num/denom to skip the division if denom is zero

    # Task 3: set just_right to true if number n is 2..5, false otherwise, without using if/else

    # Task 4: set not_right to to the opposite of of just_right

    # Task 5: rewrite not_right without using just_right, or !/not, or if/else


    # TOPIC: REGULAR EXPRESSIONS ###################################################

    # Task 1: print 'yes' if a string s begins with 'start', using regular expressions

    # Task 2: extract the *bold* word from "hello world what's *up*?" using a regular expression


    # TOPIC: CONSTANTS #############################################################

    # Task 1: define a constant to represent a maximum number of retries, set it to 15


    # TOPIC: CLASS DEFINITION, INSTANTIATION #######################################

    # Task 1: define a class Animal

    # Task 2: instantiate an Animal object


    # TOPIC: METHOD DEFINITION, INVOCATION, DEFAULT, KEYWORD & HASH ARGS, SPLAT ####

    # Task 1: on Animal, define an instance method hi, which takes a name prints hello <name>

    # Task 2: invoke the method Animal#hi

    # Task 3: set a default value 'you' for the name argument

    # Task 4: make the name argument a keyword argument

    # Task 5: define a class method Animal.pair that returns an array of two animal objects

    # Task 6: define a new global method h that takes an options hash and prints it out

    # Task 7: invoke h with options start of 3 and size of 5

    # Task 8: use the splat operator to pass the contents of an array args to a method a


    # TOPIC: PRIVATE METHODS #######################################################

    # Task 1: define a private method Animal#pretty_print which just prints a string


    # TOPIC: LOCAL AND INSTANCE VARIABLES, SCOPE ###################################

    # Task 1: in Animal#hi, assign the message string to a local variable before printing it

    # Task 2: make the Animal constructor take an argument 'name' and add "my name is <...>" to the #hi method

    # Task 3: make the animal's name accessible via a read accessor. Create an animal and prints its name

    # Task 4: make the animal's name accessible via a read/write accessor. Create an animal and change its name to Frank


    # TOPIC: INHERITANCE AND METHOD OVERRIDING #####################################

    # Task 1: define a class Dog that inherits from Animal

    # Task 2: override #hi in Dog to print 'woof'

    # Task 3: have Dog#hi take a name and call Animal#hi after printing woof

    # Task 3: define Dog#rap_name. For d = Dog.new(name: "Bob"); d.rap_name should return "B-Dog"


    # TOPIC: MESSAGE PASSING AND METHOD_MISSING ####################################

    # Task 1: write a method_missing for Dog that prints "dogs can't <method_name>"

    # Task 2: if the variable 'action' is a method name as a string or symbol, call that method on a Dog object


    # TOPIC: MODULES AS NAMESPACES AND MIX-INS, OPEN CLASSES #######################

    # Task 1: nest the Animal class inside a module Farm

    # Task 2: print the PI constant, which is in the Math module.

    # Task 3: define a module Saying with an instance method say, that prints self

    # Task 4: mix Saying into String and run the say method on the object "test string"


    # TOPIC: BLOCKS AND YIELD, PROCS ###############################################

    # Task 1: define a method salutations that prints hi, invokes its block argument with the argument 'again' and prints the result, then prints bye

    # Task 2: invoke salutations with a block that takes an argument returns a string "it's great to be here <argument>"

    # Task 3: define a variable that stores something like the previous block, then invoke salutations with that


    # TOPIC: ITERATION: EACH, MAP ##################################################

    # Task 1: print out each item in an array nums

    # Task 2: from array nums, generate a new array with each number squared


    # TOPIC: EXCEPTIONS ############################################################

    # Task 1: raise a runtime exception

    # Task 2: rescue the exception generated by 1/0 and print 'an exception happened'


    # DAS ENDE #####################################################################