DEV Community

Discussion on: Ruby devs, how do you work?

Collapse
 
rhymes profile image
rhymes • Edited

Whenever I collaborate and write Ruby code, I feel a little bit powerless

Eh eh I can see that if you're used to IDEs plus statically typed languages. I'm used to no IDE and dynamically typed languages which tend to rely on best practices and "code hygiene" on bigger code bases.

I code in Ruby all day lately, I'm going to comment adding notes about my workflow

The first thing I do when I open a project which is not my daily code base, I scroll around the directories and files, and look around.

The first thing I do in a new codebase is either try to fix a bug or read tests (or both). Reading code around, at least for me, makes it really hard to understand what fits together, mostly because code in editors is listed file by file alphabetically, so it doesn't really help :D

Another technique which I find very helpful is to set a breakpoint, hit it and then use step debugging.

Find all the usages of the class/function we are working in (get to know the context of the work);

Whenever I have to answer the question "where does this method come from" I open the terminal and grep with ripgrep which is super fast.

This does not work in Ruby (and many other interpreted languages), as any piece of code can call any other piece of code.

To be fair "calling any other piece of code" (not sure I understand the full meaning) is probably not a property of being interpreted but likely of being dynamically typed and Ruby having open classes. Ruby has encapsulation though, you can mark methods private and not be able to call them from outside without raising an exception.

Things like dependency injection are not really a thing

Ruby supports it, as all languages do. Maybe the issue here is that developers tend not to use it much and rely on mixins to inject behavior, but you can absolutely pass functions, classes and whatever using DI

How do you find out about usages or definitions of models in code that you did not write, know little about but have to refactor for some reason?

I grep for the methods, as you can see I use ripgrep quite a lot, whatever the language:

history | grep rg | wc -l
     174

Apart from test coverage, how do you make sure you do not break anything when changing something else? Even with test coverage: do you unit test every property call?

Mmm aside from test coverage? You can do manual testing but testing (be it integration, functional or unit) is still the best course of action if you want to shield from regressions.

BTW Ruby kinda has properties (if you mean the getter/setter combo). See attr_accessor. You can also customize its behavior, like custom property accessors in other languages by using meta programming, see define_method

How do you store knowledge in the code about some model, like which properties mean what, which are required during construction, which must exist together etcetera

I'm not sure I understand the question. Is it related to the concept of Java property or property of the application? In the latter case you have tests and documentation.

I fail to understand how to encapsulate something that any other piece of code can extend or modify.

The philosophy is different but you can still use access modifiers to "protect" methods. Ruby supports protected and private and they work fine. You can still call them from outside if you really need to, but at least you need to be explicit about it.

Hope this helps!