How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI

According to this site (although the information may be out of date as it is 2009 information) https://zegoggl.es/2009/05/ruby-ffi-recipes.html

When there is a function definition like below

int execvp(const char *file, char *const argv[]);

Ruby-FFI writes:

module Exec
  extend FFI::Library
  attach_function :execvp, [:string, :pointer], :int
end

When using ...

strptrs = []
strptrs << FFI::MemoryPointer.from_string("vim")
strptrs << FFI::MemoryPointer.from_string("/tmp/foo")
strptrs << nil

# Now load all the pointers into a native memory block
argv = FFI::MemoryPointer.new(:pointer, strptrs.length)
strptrs.each_with_index do |p, i|
    argv[i].put_pointer(0,  p)
end

Exec.execvp("vim", argv)

If you read it, you'll know what you're doing right away, but it's a little annoying.

If you know a better way, please leave a comment.

That's all for this article.

Recommended Posts

How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI
How to write when you want to keep line breaks and output while avoiding XSS in Rails
[java] Summary of how to handle character strings
When you want to bind InputStream in JDBI3
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
Javaer tries to summarize how to write properties in C #
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
When you want to dynamically replace Annotation in Java8
Use JLine when you want to handle keystrokes on the console character by character in Java
What you write in C # is written in Ruby like this
When you want to explicitly write OR or AND with ransack
Differences in how to handle strings between Java and Perl
When you want to change the MySQL password of docker-compose
[Ruby] How to batch convert strings in an array to numbers
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Comparison of version strings (Java implementation) when you want to branch the process between two versions
Wasteful processing of collections-I want to give you a chance to write good code. 5 [C # refactoring sample]
How to output the value when there is an array in the array
Cast an array of Strings to a List of Integers in Java
How to output a list of strings in JSF as comma-separated strings
How to write the view when Vue is introduced in Rails?
[Rails] How to write in Japanese
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
How to concatenate strings in java
A memo when you want to clear the time part of the calendar
Summary of means when you want to communicate with HTTP on Android
How to convert an array of Strings to an array of objects with the Stream API
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
[java] Summary of how to handle char
Summary of how to write annotation arguments
Convert an array of strings to numbers
[Swift] How to get the number of elements in an array (super basic)
If you want to satisfy the test coverage of private methods in JUnit
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
I want you to put the story that the error was solved when you stabbed the charger in the corner of your head