Variables And Scope In Ruby Quiz

Introduction

Welcome to our quiz! Quizzes are designed for knowledge exploration and finding weaknesses in your understanding. Use this to check up on your understanding. Even if you see the "right" answer, consider what it would take to change the given code to make it work. Think through the bugs that may arise in the given code and consider multiple ways to fix the errors. Doing so will improve your learning as there is always more than one solution and that solution may not be immediately obvious. Incidentally, those are common tasks to assign in an interview: explaining buggy code and then debugging it. Accordingly, these are skills that we hope to help you grow over your course of study at Flatiron!

Variables and Scope in Ruby Quiz

Consider the following code:

$best_dog_ever = "Byron the Moyen Poodle"

class OpinionatedClass
  def initialize(dog="Byron the Kleinpudel")
    @dog = dog
  end

  def opinionate!
    puts("#{$best_dog_ever} is the best dog ever")
  end
end

oc = OpinionatedClass.new()
oc.opinionate!

What does the $ in front of $best_dog_ever signify?

A protected variable
A static variable
A local variable
A global variable

What does .new() return (and assign to oc)?

A static copy of the OpinionatedClass class
A reference to the OpinionatedClass constructor
An instance of OpinionatedClass
An instance of OpinionatedClass with no instance variables initialized

What does oc.opinionate! print?

Trick question, it doesn't emit anything
"Byron the Moyen Poodle is the best dog ever"
"undefined is the best dog ever"
" is the best dog ever"

Your colleague is asking for some help debugging their code. When they

   oc2 = OpinionatedClass.new()
   oc2.opinionate!

they were expecting "Byron the Kleinpudel" to appear somewhere. It doesn't. Without running the code, what do you expect to see? Help your colleague understand what's going on.

In opinionate! the method returns a global versus an instance variable
In opinionate! the method returns a instance versus an global variable
@dog is in the global namespace and needs to be in the class namespace

Another colleague is asking for some help tweaking this code. When they

they want to see Novak the Goldendoodle is the best dog ever. How should they adjust the provided starter code to support this usage?

puts(@dog.to_s + " is the best dog ever")
puts("#{@dog} is the best dog ever")
puts(String(@dog) + " is the best dog ever")

Will this code work?

   oc = OpinionatedClass.new()
   oc.dog = "Zebulon T. Bark"

Works
Nope, doesn't work

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us