window.pipedriveLeadboosterConfig = { base: 'leadbooster-chat.pipedrive.com', companyId: 11580370, playbookUuid: '22236db1-6d50-40c4-b48f-8b11262155be', version: 2, } ;(function () { var w = window if (w.LeadBooster) { console.warn('LeadBooster already exists') } else { w.LeadBooster = { q: [], on: function (n, h) { this.q.push({ t: 'o', n: n, h: h }) }, trigger: function (n) { this.q.push({ t: 't', n: n }) }, } } })() Ruby 3.0. Ruby and lesser known privacy control methods - The Codest
The Codest
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Industries
    • Fintech & Banking
    • E-commerce
    • Adtech
    • Healthtech
    • Manufacturing
    • Logistics
    • Automotive
    • IOT
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About us
  • Services
    • Software Development
      • Frontend Development
      • Backend Development
    • Staff Augmentation
      • Frontend Developers
      • Backend Developers
      • Data Engineers
      • Cloud Engineers
      • QA Engineers
      • Other
    • It Advisory
      • Audit & Consulting
  • Value for
    • CEO
    • CTO
    • Delivery Manager
  • Our team
  • Case Studies
  • Know How
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
Back arrow GO BACK
2021-01-28
Software Development

Ruby 3.0. Ruby and lesser known privacy control methods

The Codest

Tomasz Szkaradek

Development Architect

One of the most beloved features of Ruby is its very flexible syntax. Personally, I love Ruby for how many possibilities we have in defining classes and their properties, and this is what I will discuss in this article.

Basic solutions

Let’s assume that we are using class Foo that has one public method and one private method:

class Foo
  def bar
    :awesome
  end
​
  private
​
  def baz
    :something_private
  end
end

Everything is great, we see such a solution in virtually every project. Running Foo.new.baz will cause the error NoMethodError (private method ‘baz’ called for # ) and that’s what we meant to do. What happens if we try to change the save format and add private as a prefix in the class definition?

class Foo
  def bar
    :awesome
  end
​
  private def baz
    :something_private
  end
end

As you can see after running the code, it actually works! Why can we enter the visibility of the method before doing it? Because when defining a method, def returns the name of the method as a symbol. This expression is not only a part of the syntax, but de facto a method derived from the Module class and treating this symbol as an argument. For more information, please see the documentation in this link. Since it started so easy with private, let’s try to change the visibility of the private method.

class Foo
  def bar
    :awesome
  end
​
  private def baz
    :something_private
  end
​
  public :baz
end

What will happen after running the code?

irb(main):012:0> Foo.new.baz
=> :something_private

Success! Our method of bases became public because we made it visible twice. Of course, the same operation applies to the modules.
​
Great, but where does it get us?
​
This functionality gives us a lot because we can freely change the visibility of a method while defining it, or even change the visibility of methods when inheriting them.

​Now, let’s take a look at what Ruby 2.7 can do in terms of changing the visibility of aliases and accessors.

class Foo
  private attr_accessor :awesome_variable
end

Unfortunately, we get an error as the private method expects symbols and attr_accessor. The code returns nil and thus this method is not compatible with the private use in Ruby 2.7. So what are our options?

  1. We can use attr_accessor under the private keyword to make it work, i.e. we will get an error when we want to refer to the awesome_variableawesome_variable method.
class Foo
  private
​
  attr_accessor :awesome_variable
end
  1. The second option is to execute the private method on methods generated by attr_attribute; in this case, we also have to remember to enter the setter there.
class Foo
  attr_accessor :awesome_variable
​
  private :awesome_variable, :awesome_variable=
end

Problems with the attr_ * methods are not the only obstacles. We can encounter the same difficulty when we want to create a private alias.

class Foo
  private alias :bar, :awesome_bar
end

Ruby 3.0 and our business

Fortunately, Ruby 3.0 introduces a great change as the visibility methods can take array as an argument and methods alias, attr_ *, can reset the array with the names of the methods that have been defined. You can read more here.

Now, let’s see some examples in the latest euba and check if the changes have actually been made and how we can use them.
​
In the first example, let’s use private before attr accessor:

class Foo
  private attr_accessor :awesome_variable
end

Such a call will not cause errors in parsing the syntax and, what is important, the awesome_variable andawesome_variable =methods become private.
​
The alias method will do the same as now it also returns a symbol as the name of the new method and makes it visible.

class Foo
  private alias :bar, :awesome_bar
end

An interesting fact is that we can also delve into further methods, e.g. the awesomeprint module can be called between private and attrreader; it is important that such a method returns an array with the names of the methods that are on the right side of the expression.

class Module
  def awesome_print(names)
    puts names
    names
  end
end
class Foo
  private awesome_print attr_reader :awesome_bar
end 

Summary

Hope you will find this article useful! In case of more news about Ruby 3.0. read more here.

Happy coding!

Ruby Developer Offer

Read more:

Shut up and take your money #1: Hidden costs and real agility in product development process

CTO challenges – scale-up and growth of software products

Related articles

Software Development

Build Future-Proof Web Apps: Insights from The Codest’s Expert Team

Discover how The Codest excels in creating scalable, interactive web applications with cutting-edge technologies, delivering seamless user experiences across all platforms. Learn how our expertise drives digital transformation and business...

THECODEST
Software Development

Top 10 Latvia-Based Software Development Companies

Learn about Latvia's top software development companies and their innovative solutions in our latest article. Discover how these tech leaders can help elevate your business.

thecodest
Enterprise & Scaleups Solutions

Java Software Development Essentials: A Guide to Outsourcing Successfully

Explore this essential guide on successfully outsourcing Java software development to enhance efficiency, access expertise, and drive project success with The Codest.

thecodest
Software Development

The Ultimate Guide to Outsourcing in Poland

The surge in outsourcing in Poland is driven by economic, educational, and technological advancements, fostering IT growth and a business-friendly climate.

TheCodest
Enterprise & Scaleups Solutions

The Complete Guide to IT Audit Tools and Techniques

IT audits ensure secure, efficient, and compliant systems. Learn more about their importance by reading the full article.

The Codest
Jakub Jakubowicz CTO & Co-Founder

Subscribe to our knowledge base and stay up to date on the expertise from the IT sector.

    About us

    The Codest – International software development company with tech hubs in Poland.

    United Kingdom - Headquarters

    • Office 303B, 182-184 High Street North E6 2JA
      London, England

    Poland - Local Tech Hubs

    • Fabryczna Office Park, Aleja
      Pokoju 18, 31-564 Kraków
    • Brain Embassy, Konstruktorska
      11, 02-673 Warsaw, Poland

      The Codest

    • Home
    • About us
    • Services
    • Case Studies
    • Know How
    • Careers
    • Dictionary

      Services

    • It Advisory
    • Software Development
    • Backend Development
    • Frontend Development
    • Staff Augmentation
    • Backend Developers
    • Cloud Engineers
    • Data Engineers
    • Other
    • QA Engineers

      Resources

    • Facts and Myths about Cooperating with External Software Development Partner
    • From the USA to Europe: Why do American startups decide to relocate to Europe
    • Tech Offshore Development Hubs Comparison: Tech Offshore Europe (Poland), ASEAN (Philippines), Eurasia (Turkey)
    • What are the top CTOs and CIOs Challenges?
    • The Codest
    • The Codest
    • The Codest
    • Privacy policy
    • Website terms of use

    Copyright © 2025 by The Codest. All rights reserved.

    en_USEnglish
    de_DEGerman sv_SESwedish da_DKDanish nb_NONorwegian fiFinnish fr_FRFrench pl_PLPolish arArabic it_ITItalian jaJapanese ko_KRKorean es_ESSpanish nl_NLDutch etEstonian elGreek en_USEnglish