ple

Ruby Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful Ruby knowledge. If you are an Anki fan, consider adding some of these questions into a deck. 😀

  1. Who created Ruby and in what year?

  2. What is the url of the Ruby Language Home Page?

  3. What is the current version of Ruby?

  4. What is the Ruby interactive interpreter called?

  5. Ruby doesn’t use curly braces for defining methods. What does it use instead?

  6. How do you write a line of text to standard output?

  7. How do you access the third command line argument of a script, run on the command line?

  8. How do you iterate through the lines of standard input?

  9. How do you write the (conditional) expression that evaluates to y if x is truthy and to z if x is falsy.

  10. How do you get the type of a Ruby expression at runtime?

  11. Why does Ruby not have primitive and reference types, like Java and JavaScript do?

  12. If everything in Ruby is an object, does that mean even integers and booleans get allocated on the heap? What allows them to not have to be heap-allocated?

  13. Does Ruby have distinct types for integers and floating point numbers?

  14. Does Ruby have distinct types for small integers and unbounded integers? Did it in the past?

  15. How can you tell whether string s is a substring of string t?

  16. How do you do string interpolation in Ruby?

  17. Why is 2 and 3 equal to 3, but 2 & 3 equal to 2?

  18. Name all the falsy values in Ruby.

  19. How do Ruby arrays respond to <, <=, >, and >=?

  20. Does Ruby have a native set type?

  21. How do you “declare” a local variable in Ruby?

  22. The hash { x: 3 } has one key and one value. What is the key?

  23. Does Ruby have an analog to JavaScript’s spread operator? If so, give an example of its use.

  24. You might often see the expression a[:], for some list a. What does this expression do, exactly?

  25. Explain why a[::-1] is the idiom for a reversed list.

  26. How does one best create an array of size 100 in which every element is 0? (Do not write a loop.)

  27. What is the difference between a+b, and [*a,*b] where a and b are arrays?

  28. How do you write an expression that adds element e to array a so that a is mutated? How do you write a non-mutating append expression?

  29. How do you add an element to the front of a list, in a mutating fashion? Is this efficient? If not, what should you do instead?

  30. Write an IIFE that applies a lambda that squares its argument to the value 100.

  31. What are the two main ways that lambda procs differ from non-lambda procs?

  32. In the method definition def f(x, y); return [x, y]; end, what do we call x and y?

  33. In the method call f(y, z), what do we call y and z?

  34. Can positional arguments ever appear after named arguments? Why or why not?

  35. Does Ruby allow assignments with array patterns on the left-hand side?

  36. Does Ruby allow assignments with dictionary patterns on the left-hand side?

  37. How do you force a function to take its arguments positionally?

  38. How do you force a function to take all its arguments as named arguments?

  39. How do you define a variable local to a block?

  40. How do you define a variable local to a function?

  41. Does Ruby have an analog of JavaScript’s temporal dead zone? If so, how does it work? If not, why is such a concept incoherent in Ruby?

  42. Write a function that takes in a number and returns a function that adds that number to its argument. Is the function you wrote higher-order? Why or why not? Does that function illustrate a closure? Why or why not? Does that function illustrate currying? Why or why not?

  43. What does Ruby call what almost every other language calls map and filter?

  44. What is the first line of that block at the end of a module that allows you to “run” the module from the command line?

  45. Instead of throw, Ruby uses **____**.

  46. The things inside of a class are called **____**.

  47. How do you create an instance of a class?

  48. What is the difference between a proc and a method?

  49. Suppose class Circle has a zero-argument method area and c is an instance of Circle. We know that c.area is actually a method call. How do we refer to the area method itself, without calling it?

  50. What is the method initialize used for?

  51. What is the Ruby analog for Python’s __str__ or JavaScript’s toString?

  52. What is the syntax for declaring a class that is a subtype of another class?

  53. How do we define class methods in Ruby?

  54. How do you allow the operators + and - to apply to instances of a class you write yourself?

  55. What is the Ruby analog of JavaScript generators (the ones made with function*).