Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Ruby: cómo agregar validación para el método << de un campo de clase

Entiendo cómo implementar un setter (validador) ( def item= ), pero ¿cómo intercepto la operación << en un campo?

 class Bla attr_reader :item def initialize @item = [] end # only called for =, +=, -= operations (not <<) def item=(value) puts "Changing value to #{value}" # pretend that there is a conditional here @item = value end # This is wrong: #def item<<(value) # puts "adding value #{value}" # @item << value #end end b = Bla.new b.item = ['one'] # works b.item += ['one'] # works b.item << 'two' # bypasses my setter

Probé def item<<(value) , eso no parece funcionar.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Cuando llama a b.item << 'two' , está llamando al método << en el item directamente. Así que tienes algunas opciones aquí:

  1. Implemente << directamente en su clase Bla , luego use b << 'two' :

     # in class Bla def <<(value) # do validation here @item << value end
  2. Use algún otro nombre de método contenedor con un nombre más agradable como add_item :

     # in class Bla def add_item(value) # do validation here @item << value end
  3. Use una clase de matriz especial para @item que tiene una definición personalizada para << :

     class MyArray < Array def <<(item) # run validation here super end end # in Bla class def initialize @item = MyArray.new end

Probablemente elegiría la opción 2, es la más simple y legible.

over 4 years ago · Santiago Trujillo Report

0

Aquí hay una sugerencia alternativa:

 class Bla # DO NOT DEFINE A READER: # attr_reader :item def initialize @items = [] end def set_items(new_items) puts "Changing value to #{new_items}" # pretend that there is a conditional here @items = new_items end def remove_item(item) # pretend that there is a conditional here @items -= items end def add_item(item) # pretend that there is a conditional here @items += items end end

Al no exponer el objeto @items directamente, mantiene el control total de la interfaz sobre cómo se manipula la variable.

(¡A menos que una persona que llama haga algo realmente extraño, como bla.instance_variable_get('@items') !!)

over 4 years ago · Santiago Trujillo Report

0

class Pit def <<(dirt) puts 'shovel ' + dirt end end Pit.new << 'sandy loam' # => shovel sandy loam
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!