loading...

Built-In Methods

rpalo profile image Ryan Palo Updated on ・1 min read

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").

Originally posted from my blog.

Posted on Apr 25 '17 by:

rpalo profile

Ryan Palo

@rpalo

Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Message me on DEV!

Discussion

markdown guide