@yegorov

4 Followers
14 Following
55 Posts

And ActiveSupport only enhances this impression with an additional Duration class, making working with time and dates even more pleasant:

```ruby
require 'time'
require 'active_support/all'

now = Time.current
now + 1.hour
now + 1.week
now + 1.month
```

What I enjoy most in Ruby is working with date and time objects.
The standard library implements time arithmetic especially elegantly: adding and subtracting seconds, days, and months.
I've never seen anything like this in any other language, where it looks so clear, concise, and beautiful:

```ruby
require 'time'

date = Time.now.to_date
date + 3 # 3 days from now
date - 3 # 3 days ago
date >> 6 # 6 months from now
date << 6 # 6 months ago
```

#Ruby #Rails #ActiveSupport #Date #Time #Duration

However, this will only work until an exception is thrown. Since the variable is not restored at the end of the method, the object's state will be corrupted. To prevent this, move the variable restoration code to the ensure block, as the code in it is always executed.

When developing instance methods in Ruby, one common problem is correctly restoring the object's state upon method completion.
For example, you need to modify instance variables for only one method call and then return them to their initial state. You could use a simple and obvious solution: save the original value in a temporary variable and then restore it at the end of the method.

#Ruby #Ensure #Method #CleanupActions #Tips

Returning to my previous post (https://mastodon.social/@yegorov/116690929213837174), in which I claimed there was no standard way to get the address of a string literal, I was using an old version of Golang, specifically 1.21. In the latest version, 1.26, it turns out you can pass a string literal to the built-in `new` function (https://go.dev/doc/go1.26#language), and it will return its address. I learned this by reviewing the gox library source code (https://github.com/icza/gox/blob/c6a8dcd8b7ea47a9a88ff920940dde82e61dbee9/gox/gox.go#L44).

#Go #String #Nil #Null #New #Gox

But what bothers me most isn't the fact that they haven't added address-receiving methods to the standard library, but rather that the maintainers' recommended approach isn't included in the standard library, forcing you to use third-party solutions like nan (https://github.com/kak-tus/nan), mo (https://github.com/samber/mo), or optional (https://github.com/markphelps/optional).
GitHub - kak-tus/nan: Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers

Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers - kak-tus/nan

GitHub
But I'm not alone in this problem, as this issue, which proposed adding a method for obtaining the address of a literal to the standard library, was discussed (https://github.com/golang/go/issues/38298) back in 2020.
I understand the maintainers' position, which is to avoid using pointers for optional values in structs (and if you want to use them, implement these methods yourself). To some extent, I agree with them and support their choice.
proposal: pointer package · Issue #38298 · golang/go

My apologies if this is a duplicate proposal, but the keywords are too generic to make search useful. Description Due to addressability rules, some literals cannot have pointers made of them inline...

GitHub

Whenever I return to programming in Golang, I always encounter the lack of a standard solution for obtaining the address of a string literal. This is especially necessary when initializing a pointer to a string in a structure, for example, when it contains an optional string field.

#Go #String #Nil #Null #Optional #NullType

Next, I run it in the console, for example like this:
```
bin/rails r 'CheckJob.perform_later'
```

This check can also be extended by adding execution of this task to the cron using the whenever gem:
```
every 30.minutes do
runner 'CheckJob.perform_later'
end
```

And of course, you can not only write a message to the log, but also send a notification to your monitoring system.

One common problem when working with background tasks in Rails is ensuring that the task queue itself is configured and functioning correctly.
For this purpose, I always create a test job that simply writes a test message to the log:
```
class CheckJob < ApplicationJob
queue_as :default

def perform
logger = Logger.new(Rails.root.join('log', 'check.log'), level: :info)
logger.info('OK')
end
end
```

#Ruby #Rails #ApplicationJob #Job #Task #Background #Cron #Whenever #Check