Cover Image

Built-In Methods

289 words. Time to Read: About 2 minutes.

This is just going to be a quick one. Recently, I was working on something (that I will discuss in an upcoming post) on an airplane. I was using Ruby. Obviously I’m not going to fork out five whole dollars for the onboard internet access, so I was roughing it. I came across an error.

if line.contains? "tags: "
    # Do stuff

Wham. undefined method contains? for String. Darnit! I had just gotten done programming in Python and I hadn’t programmed in Ruby for a little while, so I rewrote it.

if "tags: " in line
    # Do stuff

NOPE. Unexpected keyword_in. Bah! I knew there was a method here, but I couldn’t for the life of me remember what it was. My first instinct was to immediately Google the Ruby String documentation, but – remember – no internet. Out of hope and despiration, with a quick prayer to the coding gods, I entered the following into the terminal:

$ irb
# Typical IRB headers
>>> "Potato".methods
[:include?, :%, :*, :+, :unicode_normalize, :to_c, :unicode_normalize!, :unicode_normalized?, :count, ...

There! Right at the front was what I needed. No Googling required. So the next time you reach for Google for a question, try asking your code first!

if line.include? "tags: "
    # profit
end

In case you’re interested, you can do the same thing in Python via dir("potato").

Author: Ryan Palo | Tags: ruby tricks | Buy me a coffee Buy me a coffee

Like my stuff? Have questions or feedback for me? Want to mentor me or get my help with something? Get in touch! To stay updated, subscribe via RSS