Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code example in getting started guide #14

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

ayushn21
Copy link
Contributor

The code example in the getting started guide wasn't working for me. It looks like there were some API changes causing it to break. I've amended it to a version that worked for me.

Please let me know if there's a better way to accomplish this. I just hammered at the code until it worked.

Types of Changes

  • Documentation

Contribution

@ayushn21
Copy link
Contributor Author

On a tangent, I had to manually require 'async/rest/wrapper/form' even though I've got require 'async/rest'. Is there a way to maybe autoload the wrappers when they're referenced or something like that? Seems a bit fiddly to have to require it manually.

@ayushn21
Copy link
Contributor Author

I've simplified the code so the above comment is now outdated.

I reckon the general question still applies though.

@ioquatix ioquatix merged commit 2caba2f into socketry:main Jan 3, 2025
@ioquatix
Copy link
Member

ioquatix commented Jan 3, 2025

Thanks!

@ioquatix
Copy link
Member

ioquatix commented Jan 3, 2025

I'd like to add support for autoload but we need autoload_relative :p

@ayushn21 ayushn21 deleted the fix-doc-example branch January 4, 2025 03:10
@ayushn21
Copy link
Contributor Author

ayushn21 commented Jan 4, 2025

I'd like to add support for autoload but we need autoload_relative :p

Ah ... yeah it's a bit fiddly to specify the full path .... I just tried a few things and got it working as shown in the below commit. Is there any reason we shouldn't do it that way?

ayushn21@237711b

@ioquatix
Copy link
Member

ioquatix commented Jan 4, 2025

If we start going down that path, it's a model we should adopt everywhere. I'm not sure it's the right path yet. That's my reservation from doing it here. @fxn might have an opinion about it. I know that being explicit is kind of annoying too (pedantic).

In other words, if we adopt autoload (and it needs to be relative), I'd like to know for sure that it's the right path forward for all my gems.

As an alternative, I'm okay with the top level file, e.g. lib/async/rest.rb to require all the convenient stuff and/or lib/async/rest/wrapper.rb doing something similar just for the wrapper code files.

@ayushn21
Copy link
Contributor Author

ayushn21 commented Jan 4, 2025

Yeah fair enough 👍

@fxn
Copy link

fxn commented Jan 4, 2025

Hey! I have done a quick look at the source code. The library is small and most of it is eager loaded, I also see it would be convenient that users do not have to do that require by hand and, unless there is something that says that is a bad idea, eager loading this file seems fine to me. That is, no need for autoload if you will in my opinion.

Since we are on it, let me share also the way I normally structure my code when it does not use autoload. My main driver is to be intentional about who creates the namespaces: It is the file that naturally defines them. So, my code looks like this:

# foo.rb
module Foo # The constant is create here.
  require_relative 'foo/bar'
  require_relative 'foo/baz'
  ...
end

# foo/bar.rb
module Foo
  class Bar # The constant is created here.
    require_relative 'bar/woo'
    ...
  end
end

Just sharing for curiosity, not meaning other people should do this :).

I like the clarity in the ownership of constant creation, and also allows files downwards to freely choose is they want to define things using a constant path (class Foo::Bar ... end) or nesting.

(Exception is allowing lib/foo/version.rb to create Foo if loaded from the gemspec, but that is minor.)

@ioquatix
Copy link
Member

ioquatix commented Jan 5, 2025

Okay, I've added 1275314

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants