Latest Posts (10 found)
(think) 1 months ago

Why I Chose Ruby over Python

This year I spent a bit of time playing with Python, after having mostly ignored it since 2005 when was learning it originally. I did like Python back then, but a few years afterwards I discovered Ruby and quickly focused my entire attention on it. There were many (mostly small) reasons why I leaned towards Ruby back then and playing with Python now made me remember a few of them. I thought it might be interesting to write a bit about those, so here we go. Disclaimer: This is not a rant and I know that: So, treat this as an amusing personal account and nothing more than that. Probably the thing about Python that bothered me the most what stuff that would normally be methods are global functions (that often call some object methods internally). I’m referring to the likes of: You can find the full list here . I’m guessing the reason for this (as usual) is historical, but I much prefer the way Ruby does things. E.g. instead of or vs . is a pretty weird beast as it can: Why would someone want a keyword for removing items from lists and dictionaries instead of some method is beyond me. Ruby’s arrays have methods like: In Ruby almost everything is an expression (meaning that evaluating it would result in a value). In Python a lot of things are consider “statements” - something executed for their side effects only. If you haven’t used languages like Ruby or Lisp this might sound a bit strange, but if we go back to the previous section about , we can observe that: That’s something that I really value and I consider it one of the bigger practical advantages of Ruby over Python. At first I thought the semantic indentation used by Python is super cool, as it reduces a bit the typing one needs to do: In hindsight, however, I quickly realized that it also: One more thing - 4 spaces by default seems a tad too much to me, although that’s obviously debatable. P.S. I feel obliged to admit I’m not a big fan of either and would have preferred instead, but it is how it is. I’m not a fan of Python’s type as for me it’s a pretty weird duck: I get how things ended up the way they are, but for me it’s not OK to be able to write code like . I’m also not a fan of treating empty collection literals as , although I definitely have less issues with this than with 0 and 1. To compare this with Ruby: This definitely resonates better with me. Side note: Lately I’ve been playing a lot with languages like OCaml, F# and Rust, that’s why to me it now feels extra strange to have a boolean type that works like an integer type. I really like the range literals in Ruby: Python has the function that kind of gets the job done, but for whatever reason it doesn’t have the option to mark something as inclusive range. Definitely not a big deal, but one of the many small touches of Ruby’s syntax that I came to appreciate over time. In Python one has to pass to instance methods explicitly, which always seemed to me like an excessive level of verbosity. Also, that’s quite uncommon in other object-oriented programming languages. Many special methods have names surrounded with , which I find both odd and not very easy to type. I get why this was chosen (to avoid naming conflicts), but I don’t like it regardless. I really like that in Ruby the return value of a method is the value of the last expression that got evaluated in the method. There’s a expression in Ruby, but it’s rarely needed in practice. In Python, by comparison, you always have to use , otherwise your method will return . Not a big deal in general, but as I spend a lot of time with Ruby and various functional programming languages, it’s definitely something that bothers me. Admittedly that’s a very small one, but I would have preferred if anonymous functions were created with a keyword like or instead of . In Ruby historically they were created with as well, but afterwards the shorthand was introduced as well. There’s nothing wrong with the Python syntax per se, but I think that in general for lambdas it’s better to have a more compact syntax. Ruby predicates typically have names ending in - e.g. , , . This makes them really easy to spot while reading some code. Python sticks to the more common convention of prefixing such methods with , , etc and that’s fine. One thing that bothers me a bit is that often there’s not spaces between the prefix and the rest of the name (e.g. ), which doesn’t read great in my opinion. More importantly, in Ruby and Python it’s common to have destructive and non-destructive versions of some methods. E.g. - vs in Ruby, and vs in Python. I don’t know about you, but to me it seems that: I’m guessing the reasons here are also historical. Multi-line text literals are common in many languages, but I’m not super fond of: Who thought that typing those would be easy? It’s not that HEREDOCS in Ruby are great either, but I guess they are at least more common in various programming languages. Ruby has and . That’s it. Everyone uses them. Life is simple. Things are a lot more complicated in the realm of Python where several different tools have been in fashion over the years: Now it seems that might replace them all. Until something replaces I guess… And that’s a wrap. I’m guessing at this point most Rubyists reading this would probably agree with my perspective (shocking, right?) and most Pythonistas won’t. And that’s fine. I’m not trying to convince anyone that Ruby’s a better language than Python, I’m just sharing the story of how I ended up in team Ruby almost 20 years ago. Back in the day I felt that Ruby’s syntax was more elegant and more consistent than Python’s, and today my sentiment is more or less the same. Don’t get me wrong, though - I like Python overall and enjoy using it occasionally. It just doesn’t make me as happy as Ruby does. I’ve long written about my frustrations with Ruby, so it feels pretty good to write for once about the aspects of Ruby that I really enjoy. Keep hacking! P.S. After writing this I realized I had already written a similar article 14 years ago, but I had totally forgotten about it! Oh, well… the things I prefer in Ruby over Python are super subjective for every thing that Ruby does “better” there’s something else that Python does better delete variables dictionary items (to remove some values) (to remove some index) There’s no obvious way to determine if actually removed something In Ruby, however, most deletions result in informative results makes things harder for tooling, as it can’t really rearrange indented code sections in editors you can’t have much in terms of auto-indenting as you type (as the editor can’t know when a block finishes without you outdenting explicitly) was added only in Python 2.3 it inherits from and are essentially 1 and 0 has nothing to do with numbers there the only things are considered logically false are and (inclusive range) (exclusive range) Ruby encourages you to favor the non-destructive versions of the methods, unlike Python Ruby’s more consistent than Python

0 views
(think) 1 months ago

Wayward Predicates in Ruby

Recently I’ve been wondering how to name Ruby methods that have predicate looking names (e.g. ), but don’t behave like predicates - namely they return other values besides the canonical and . 1 Here are a few classic examples: Naming is hard and it took me a while to narrow my list of ideas to two options: In the end I felt that “wayward predicates” was the way to go, as it sounds less deviant. 2 Common vocabulary is important when discussing domain-specific topics and it makes it easier for people to refer to things within the domains. That’s why I’m writing this short article - perhaps it will be a first step to establishing a common name for a pretty common thing. I’ve always been a believer that naming should be both informative and fun, and that’s certainly one fun name. Wayward predicates can surprise us unpleasantly every now and then, but they are also a classic example of the flexibility of Ruby and its continued resistance to sticking to narrowly defined rules and guidelines. That’s all I have for you today. Keep hacking! This came up in the context of RuboCop, in case someone’s wondering.  ↩ See also https://docs.rubocop.org/rubocop/cops_naming.html#namingpredicatemethod for the first “official” usage of the term.  ↩ wayward predicates deviant predicates This came up in the context of RuboCop, in case someone’s wondering.  ↩ See also https://docs.rubocop.org/rubocop/cops_naming.html#namingpredicatemethod for the first “official” usage of the term.  ↩

0 views
(think) 1 months ago

How to Vim: Fixing Typos

Here’s another small Vim tip - how to deal with typos quickly. Generally, most people do something along those lines: It’s a pretty sound approach overall and there’s nothing wrong with it. Still, because I see the typos I make as I’m typing when the spell-checker is enabled, I think there are two other reasonable ways to approach the problem at hand: I use both of those approaches as type, but most often I lean towards 2), as for longer words it’s a bit more efficient. By the way, here’s my spell-checking configuration: Sadly, unlike some other editors you can’t use Vim to spellcheck only code comments (in Emacs that’s easily achieved with ), so I limit the use of to text types I’m working often with (e.g. Markdown). Funny enough, I had to use the advice I shared in this post several times while writing it. I hope that no typos made it in the final version! That’s all I have for you today! Keep hacking! enable (the built-in spell-checking) use and to move between the previous/next misspelled word correct the typo with or If you’re a fast touch typist you can just to delete the preceding word and retype it from scratch. Using for single-letter corrections is fine as well, of course. Use to immediately trigger smart completion for the last misspelled word, using suggestions from your spelling dictionary.

0 views
(think) 1 months ago

How to Vim: Take Control of Split Positioning

One of my pet peeves with Vim is that by default the buffer splitting behaves a bit weird: That’s exactly the opposite of how things happen by default in Emacs (and a few other editors), so it really didn’t sit well with me. Obviously you can use dedicated commands to control the split behavior (e.g. and 1 ), but if you want to permanently “improve” the defaults just add the following your : In case it’s not clear: And that’s it! Now I find using commands like a lot more enjoyable, as the help splits appear exactly where I expect them to. Those commands are pretty crazy in the typical spirit of Vim and you can read more about them here .  ↩ horizontal splits ( ) appear on top vertical splits ( ) appear on the left alters the behavior of (and commands like that use internally) alters the behavior of Those commands are pretty crazy in the typical spirit of Vim and you can read more about them here .  ↩

0 views
(think) 2 months ago

Learning OCaml: Having Fun with the Fun Module

When I started to play with OCaml I was kind of surprised that there was no (identity) function that was available out-of-box (in module, that’s auto-opened). A quick search lead me to the Fun module, which is part of the standard library and is nested under . It was introduced in OCaml 4.08, alongside other modules such as , and . 1 The module provides a few basic combinators for working with functions. Let’s go over them briefly: The identity function: returns its single argument unchanged. Returns a function that always returns the first argument, ignoring its second argument. Composes two functions, applying the second function to the result of the first. Haskell and F# have special syntax for function composition, but that’s not the case in OCaml. (although you can easily map this to some operator if you wish to do so) Also, introduced a bit later than the other functions in the module - namely in OCaml 5.2. Reverses the order of arguments to a two-argument function. Negates a boolean-returning function, returning the opposite boolean value. Useful when you want to provide a pair of inverse predicates (e.g. and ) I believe that those functions are pretty self-explanatory, but still below we’ll go over a few examples of using them: Admittedly the examples are not great, but I hope they managed to convey how to use the various combinators. Those are definitely not the type of functions that you would use every day, but they can be useful in certain situations. Obviously I needed at some point to discover the module in the first place, and all of the functions there can be considered “classic” combinators in functional programming. In practice most often I need and , and infrequently and . Right now I’m struggling to come up with good use-cases for , but I’m sure those exist. Perhaps you’ll share some examples in the comments? How often do you use the various combinators? Which ones do you find most useful? I find myself wondering if such fundamental functions shouldn’t have been part of module directly, but overall I really like the modular standard library approach that OCaml’s team has been working towards in the past several years. 2 The important thing in the end of the day is to know that these functions exist and you can make use of them. Writing this short article will definitely help me to remember this. That’s all I have for you today. Keep hacking! It was part of some broader efforts to slim down and move in the direction of a more modular standard library.  ↩ And obviously you can open the module if you wish to at whatever level you desire.  ↩ The identity function: returns its single argument unchanged. Returns a function that always returns the first argument, ignoring its second argument. Composes two functions, applying the second function to the result of the first. Haskell and F# have special syntax for function composition, but that’s not the case in OCaml. (although you can easily map this to some operator if you wish to do so) Also, introduced a bit later than the other functions in the module - namely in OCaml 5.2. Reverses the order of arguments to a two-argument function. Negates a boolean-returning function, returning the opposite boolean value. Useful when you want to provide a pair of inverse predicates (e.g. and ) It was part of some broader efforts to slim down and move in the direction of a more modular standard library.  ↩ And obviously you can open the module if you wish to at whatever level you desire.  ↩

0 views
(think) 2 months ago

Learning OCaml: Numerical Type Conversions

Today I’m going to cover a very basic topic - conversions between OCaml’s primary numeric types and . I guess most of you are wondering if such a basic topic deserves a special treatment, but if you read on I promise that it will be worth it. So, let’s start with the basics that probably everyone knows: Both functions live in module, which is opened by default in OCaml. Here it gets a bit more interesting. For whatever reasons there’s also a function, that’s a synonym to . There’s no function, however. Go figure why… Here’s a bit of trivia for you - does truncation to produce an integer. And there’s also a function that’s another alias for . Again, for whatever reasons it seems there are no functions that allow you to produce an integer by rounding up or down. (although such functions exist for floats - e.g. , and ) More interestingly, OCaml 4.08 introduced the modules and that bring together common functions for operating on integers and floats. 1 And there are plenty of type conversion functions in those modules as well: The introduction of the and modules was part of (ongoing) effort to make OCaml’s library more modular and more useful. I think that’s great and I hope you’ll agree that most of the time it’s a better idea to use the new modules instead of reaching to the “historical” functions in the module. Sadly, most OCaml tutorials out there make no mention of the new modules, so I’m hoping that my article (and tools like ChatGPT) will steer more people in the right direction. If you’re familiar with Jane Street’s , you’ll probably notice that it also employs similar structure when it comes to integer and float functionality. Which type conversion functions do you prefer? Why? That’s all I have for you today. Keep hacking! Technically speaking, existed before 4.08, but it was extended then. 4.08 also introduced the modules , , and . Good stuff!  ↩ you can convert integers to floats with you can convert floats to integers with Technically speaking, existed before 4.08, but it was extended then. 4.08 also introduced the modules , , and . Good stuff!  ↩

0 views
(think) 4 months ago

zoxide: tips and tricks

zoxide is a smart and fast alternative to that learns your directory usage patterns and allows you to jump to directories quickly. It’s one of my favorite command-line tools and it’s an essential part of my workflow. Here are some tips and tricks to get the most out of zoxide: Normally you’d use to trigger , but pressing is not super convenient. That’s why I like to use instead. You can set this up by adding the following to your shell configuration file (e.g., , ): It took me a while to figure that wasn’t the optimal keybinding for and I’m really happy with . If you have installed, the command can be used to interactively select a directory to jump to. is another thing that’s not super easy to type, so I suggest using the alias instead: was inspired by and , but unlike them it’s a complete replacement for . That’s why it’s not a bad idea to simply alias to : Note that uses internally, so switching fully to it won’t mess with any of Fish’s internally directory stack. (e.g. and ) If you have multiple directories with similar names you can use SPACE to trigger smart completion for your options: This will present the matching candidates (using if present), and you can select one to jump to it. If you’re a Fish user (like me), you can get nice TAB completions by installing the following Fish plugin: Now try the following: You can read more about the plugin here . And that’s a wrap! Even though I do all of the above, I think the first two items are the most important. and are very easy to type and cover all of my use-cases beautifully. If you have any other tips or tricks for zoxide, feel free to share them in the comments below! That’s all I have for you today! Keep hacking! Normally you’d use to trigger , but pressing is not super convenient. That’s why I like to use instead. You can set this up by adding the following to your shell configuration file (e.g., , ): If you have installed, the command can be used to interactively select a directory to jump to. is another thing that’s not super easy to type, so I suggest using the alias instead: was inspired by and , but unlike them it’s a complete replacement for . That’s why it’s not a bad idea to simply alias to : If you have multiple directories with similar names you can use SPACE to trigger smart completion for your options: If you’re a Fish user (like me), you can get nice TAB completions by installing the following Fish plugin:

0 views
(think) 4 months ago

How to Fish: Moving Between Recent Directories

I love Fish, because it makes a lot of common everyday tasks easier and more convenient. One such task is moving (switching) between folders you’ve visited recently. In my case I often jump between project directories, configuration directories, etc. Historically I’ve used things like , / and / , but ever since I moved to Fish I realized all I needed was covered by the following built-in commands: Basically, things that require some manual setup/operations in other shells are handled automatically in Fish, as it maintains a list of the last 25 visited folders out-of-the-box. 1 Here’s how using looks like: Now you can press or to switch to . I mostly use and (via their keybindings), but is quite handy as well. Admittedly keybindings that use arrow keys are not ideal on every keyboard (as some compact keyboards don’t have arrow keys), but those can be easily remapped to whatever keys you fancy. At this point I see no need for external tools to help with the directory history management, although you have to keep in mind that the directory history in Fish is not persistent, so you might still find some value in / and friends. That’s all I have for you today! Happy Fishing! You can read more on the subject here .  ↩ ( ) - takes you to the previous directory ( ) - takes you to the next directory ( ) - shows a list of the recently visited directories - allows you to quickly jump to any recently visited directly using letter or number shortcuts You can read more on the subject here .  ↩

0 views
(think) 4 months ago

How to Vim: Proper Ways to Escape

(Escape) is one of the most central key in the world of Vim. It takes you from Insert mode to Normal mode and it also serves to interrupt operations in progress in Vim. You’ll be using it a lot! Vim has a very long history . It’s a product of a different era where the keyboard layouts were quite different as well. Below is a picture of the legendary ADM-3A, which Bill Joy famously used when he created vi: The problem with ESC, however, is that on many modern keyboards it’s not the easiest key to reach. Fortunately, you have plenty of options to improve the situation on that front. Let’s go over them in order of increasing complexity (and price). Pressing is exactly the same as pressing ESC as far as most terminals are concerned, and for many people (me included) that’s a lot easier to press than reaching for ESC at the top-left corner of my keyboard. This becomes even more convenient if you remap your mostly useless Caps Lock key to Control. There’s also that you can consider using. When invoked in insert mode it behaves almost like ESC, with a couple of caveats that you need to be aware of: Quit insert mode, go back to Normal mode. Do not check for abbreviations. Does not trigger the autocommand event. In many languages certain sequences of characters are quite uncommon (e.g. ), so those are something you can bind in insert mode to serve as exit. is a classic, with other popular options being and . Those certainly work well in English, but with other languages your mileage will vary. On top of this - in other contexts you’ll still need to press ESC, Control+[ or Control+c. I kind of like an uniform approach to the Escape problem. Continuing the line of reasoning from above, we can go a bit further and actually use a keyboard remapping tool to have our (left) Control key behave as Control when held down and as ESC when tapped (pressed quickly). That’s actually what I’m doing and I think that’s the best possible setup. These days I’m mostly using macOS and the popular Karabiner Elements keyboard remapper. For it you’ll need to add something like the snippet below to your configuration file: For optimal results - make Caps Lock your left Control. Some keyboards are more vim-friendly than others. By this I mean they place ESC where normally is, which makes pressing ESC a lot easier. A couple of classic examples are: On top of this, the HHKB replaces Caps Lock with Control directly, so there’s one less remapping you have to do yourself. The keyboards I’ve mentioned are on the expensive side, but they are legendary in the programming community for a reason. I’m not a fan of this approach, though, as I’m extremely used to the placement of the and it’s commonly used in many programming languages. 2 My Leopold offered a middle way by having the on a different layer, but I still preferred to have it be a by default. Of course, your mileage here will vary, based on your personal habits and preferences. Some people swear by their keyboards with “programmer layout” and that’s fine. And that’s a wrap! You certainly have some options to consider, but for me a dual-function Control/ESC, placed where Caps Lock normally is, is the way to go. That’s all I have for you today. Keep hacking! My recent purchase of the HHKB was part of my inspiration to play again with Vim. I think it’s clear that the keyboard was designed with Vim users in mind.  ↩ My main issue with non-standard layouts is that I still have to use laptop keyboards and they keyboards of some colleagues/friends from time to time.  ↩ HHKB (my current keyboard) 1 Leopold FC660 (my former keyboard) My recent purchase of the HHKB was part of my inspiration to play again with Vim. I think it’s clear that the keyboard was designed with Vim users in mind.  ↩ My main issue with non-standard layouts is that I still have to use laptop keyboards and they keyboards of some colleagues/friends from time to time.  ↩

0 views
(think) 4 months ago

How to Vim: Reloading File Buffers

In Vim (and many other editors) we interact with the contents of files via the “file buffer” abstraction. Basically, that’s the in-memory representation of a file within a text editor, that occasionally gets synchronized with the disk one (the actual file). From time to time a file might get changed outside Vim (e.g. you had it changed in another editor or you pulled some updates from your VCS). In those cases we usually want to reload the file contents into the file buffer. There are multiple ways to do this in Vim (shocker, right) - a manual (with a couple of nuances) and an automated approach (with many nuances). Let’s start with the manual, as it’s super simple. Just press (dit) followed by to reload the current buffer. You can also use to discard any changes you may have made. I’m more partial to the automated way, which involves enabling . Just add it to your like this: Unfortunately, for whatever reasons, this won’t get triggered in all desirable cases, so we’ll need to help Vim a bit with some extra configuration that checks the file modification time when the editor gains focus or we switch to a buffer: One final thing to keep in mind - you can go back to your version of the file with (undo). So, you don’t have to worry that any local edits will get permanently lost of you enable . So, that’s crux of reloading files in Vim - not exactly trivial, but perhaps a bit more complicated than it needs to be. Down the road I’ll have to check if that’s made simpler in Neovim. That’s all I have for you today. Keep hacking!

0 views