Rubber Ducking

So, um, this is a thing. If you’re stuck on a coding problem, you can try “rubber ducking” – pretend that you have a rubber duck on your desk, and try to describe to the duck what you’re trying to do, and what’s going wrong.

Sometimes, just talking through the problem (even if the conversation is imaginary), you can get a better perspective on the issue, and be able to resolve it.

File importing tip

In ruby, you can import a file into your class, by putting:

require "name_of_the_file_without_the_extension"

at the top of your class definition file. This allows you to import another class file into this class file, so you can access the methods in the other class. Pretty cool.

BUT, it can only import ruby files. Do NOT try to require other file types, because, in the background, ruby expects the file name to end with .rb. If it is a different kind of file, ruby will try to import a file with the file’s filename, but with the .rb extension. So, if you have a file named:

phone_numbers.txt

And you add this to a ruby class file:

require “phone_numbers”

ruby is going to throw an error when you try to use that class file. It’ll look something like:

/ridiculously/long/path…/custom_require.rb:36:in `require’: cannot load such file — words

If you need to use a different kind of file, pass its file name into initialize.

def initialize(filename)
   @filename = filename
end

Things that aren’t obvious

In ruby, pretty much everything is an object. If you create an integer, say, 5, that’s an object. And there are methods that can be run on that object.

As an integer, 1 is an object in the “fixnum” class. You don’t have to tell this to ruby, it’s capable of figuring that out all on its own. This means you can do things to the number 1 using methods from the fixnum class, like this:

1.upto(10)

upto is a method in the fixnum class that repeats the code below it for the range of times between the number to the left of the dot and the number in parens. So:

1.upto(10)
   some_code_I_wrote

Will run some_code_I_wrote 10 times.

Magical Kingdom of CSV Wonder

Since working with CSV files (comma separated values) is so common, Ruby has a whole library of methods designed just for that purpose.

Require the csv library at the top of your file to get access to this magical kingdom of wonder:

require “csv”

Then you can call built-in methods, instead of rolling your own. Here’s one that iterates over each item in a csv file:

CSV.foreach(filename, some_optional_stuff)

Predicate and Bang Methods

Ruby allows you to use non-alphanumeric characters in method names. There are a couple that are used fairly commonly: ? and !

You put the character at the end of the method name.

Predicate

? indicates a “predicate” method. This is a method that returns true or false. So, for example, if you want to know if the result of a particular operation is true or false, you can create a method that does that operation, then returns true or false based on the result of the operation.

For example, if you want to know if the player won a game, you could make a method called “won?”

Then use it like:

if won?
   puts “Congratulations, you won!”
else
   puts “Sorry, you lost.”

It’s a great way to make code that almost reads like English.

Bang

A bang method tells you that this method does something “dangerous” (changes a value, dun, dun, duu-uu-un).

def turns_remaining!
   @turns_remaining -= 1 # decrements variable
end

First Day of Metis

Did an overview of HTML and CSS

Did you know it’s technically “illegal” in HTML to have more than one H1 on a page? I had no idea. Has that always been in the standard?

Skip to content