Ruby Weekly Issue 563: July 29, 2021



Learn, Grow and Empower Your Engineering Team — Grow your leadership skills and build a healthy team culture that delivers business results. Our Leadership Hub, Workshops, and Tech Leadership Coaching help new and experienced managers anticipate and overcome everyday challenges.

Blackmill

RubyMine 2021.2 Released — RubyMine is a popular (but commercial) Ruby IDE offering refactoring, debugging, code assistance, and numerous other conveniences out of the box. This new version can lean on Ruby 3’s RBS signatures to provide even better code completion.

JetBrains

Making Rails Run Just a Few Tests Faster — Jorge created a (now merged) pull request to Rails that only enables parallel testing if the number of tests is above a threshold that justifies the overhead.

Jorge Manrubia (Basecamp)

IN BRIEF:

Find Ruby Jobs with Hired — Create a profile on Hired to connect with hiring managers at growing startups and Fortune 500 companies. It’s free for job-seekers.

Hired

📕 Articles & Tutorials

All You Need to Know About Netcat (nc) — This isn’t about Ruby specifically but if you’re on a POSIX-compliant OS you almost certainly have nc available and it’s well worth knowing how to use the “TCP/IP / networking swiss army knife.”

Michael Ikua

Webpack All the Assets? — With the release of Rails 6, Webpack was introduced as the default JavaScript bundler but what about using it to handle all the assets in an app?

Ariel Juodziukynas

How to Ignore ‘Bullet’ in RSpec TestsBullet is a tool for monitoring your app’s queries to find inefficiencies you can resolve in your code. But in testing it can sometimes have false positives you need to silence.

Tony Osbourn

▶  Discussing Observability with Charity Majors — Charity Majors, the co-founder and CTO of the Honeycomb observability platform, Co-founder and CTO of Honeycomb, an observability platform, digs deep into observability, its importance on a project, and how to do it right.

The Bike Shed Podcast podcast

💡 Tip of the Week

Ancestors in Ruby

Ruby’s object model is fascinating! In the next few tips following this one, we’ll look at different ways of using code from a module in a class: include, extend and prepend.

In order to set ourselves up to understand the nuances behind each approach, we need to first understand Ruby’s concept of ancestors, and how they are used. We’ll use a small empty Example class to start:

class Example
end

Now let’s call #to_s on an instance of a Example:

Example.new.to_s
=> "#<Example:0x00007fcc5997ec30>"

How did this happen? We never defined Example#to_s. Ancestors are an ordered array of classes or modules that are included or prepended in a given module or class. Let’s break that down. In this case, we can see Example#ancestors:

Example.ancestors
=> [Example, Object, Kernel, BasicObject]

When Ruby is looking for a method defined on an object, it will traverse these ancestors in order looking for the method. So in our example, Ruby will not see #to_s defined on Example, and will traverse to look for it defined on Object. It is defined on Object, so Ruby uses that!

If we, however, do define it within Example:

class Example
  def to_s
    "We created this!"
  end
end

Example.new.to_s
=> "We created this!"

We can confirm it will use our definition of #to_s within Example before defering to looking for #to_s later in the ancestors chain. Check out next week’s tip to see how ancestors are relevant to include!

This week’s tip was written by Jemma Issroff.



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here