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

Add sym_defined? methods to test if a symbol is defined #108

Merged
merged 4 commits into from
Sep 13, 2022

Conversation

tenderlove
Copy link
Member

I would like to check if a symbol is defined before trying to access it. Some symbols aren't available on all platforms, so instead of raising an exception, I want to check if it's defined first.

Today we have to do:

begin
  addr = Fiddle::Handle.sym("something")
  # do something
rescue Fiddle::DLError
end

I want to write this:

if Fiddle::Handle.sym_defined?("something")
  addr = Fiddle::Handle.sym("something")
  # do something
end

Before this commit, missing symbols would raise an exception.  Now we
can check if a symbol exists without raising an exception.
ext/fiddle/handle.c Outdated Show resolved Hide resolved
This just adds a typedef to make the C code a bit easier to read
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

ext/fiddle/handle.c Outdated Show resolved Hide resolved
ext/fiddle/handle.c Outdated Show resolved Hide resolved
tenderlove and others added 2 commits September 13, 2022 08:27
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
@tenderlove tenderlove merged commit 9d3371d into master Sep 13, 2022
@tenderlove tenderlove deleted the sym-defined branch September 13, 2022 15:28
matzbot pushed a commit to ruby/ruby that referenced this pull request Oct 7, 2022
…ed (ruby/fiddle#108)

I would like to check if a symbol is defined before trying to access it.
Some symbols aren't available on all platforms, so instead of raising an
exception, I want to check if it's defined first.

Today we have to do:

```ruby
begin
  addr = Fiddle::Handle.sym("something")
  # do something
rescue Fiddle::DLError
end
```

I want to write this:

```ruby
if Fiddle::Handle.sym_defined?("something")
  addr = Fiddle::Handle.sym("something")
  # do something
end
```

ruby/fiddle@9d3371de13

Co-authored-by: Sutou Kouhei <kou@clear-code.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants