Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin's With, for ruby

Tags:

ruby

kotlin

Is there any function or way to simulate this with in ruby?

It helps you to organize the code that acts over the same object, something like:

with(callDao) {
            whenever(deleteAll()).thenComplete()
            whenever(insertAll(any())).thenComplete()
}

vs

            whenever(callDao.deleteAll()).thenComplete()
            whenever(callDao.insertAll(any())).thenComplete()
like image 974
Daniel Gomez Rico Avatar asked Feb 06 '26 09:02

Daniel Gomez Rico


1 Answers

Yes, you can build your own with using instance_eval.

def with(obj, &block)
  obj.instance_eval(&block)
end

with(" banana ") {
  puts strip
  puts reverse
}

Output:

banana
 ananab 
like image 60
Jared Beck Avatar answered Feb 09 '26 08:02

Jared Beck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!