https://github.com/j8r/crystal-object-send
Interpret a String to an Object method call
https://github.com/j8r/crystal-object-send
crystal-lang method send string
Last synced: 10 months ago
JSON representation
Interpret a String to an Object method call
- Host: GitHub
- URL: https://github.com/j8r/crystal-object-send
- Owner: j8r
- License: isc
- Created: 2019-02-12T13:39:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-09T23:35:36.000Z (over 6 years ago)
- Last Synced: 2025-05-12T22:55:19.966Z (10 months ago)
- Topics: crystal-lang, method, send, string
- Language: Crystal
- Size: 8.79 KB
- Stars: 13
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Crystal Object#send
[](https://cloud.drone.io/j8r/crystal-object-send)
[](https://en.wikipedia.org/wiki/ISC_license)
Interpret a String to an Object method call.
Similar to the Ruby's `Object#send`.
Here macros and the `Crystal::Parser` are used to build a pseudo interpreter.
## Disclaimer
There are lots of limitations, this library is mainly an experiment.
Usually you better avoid using it and interpret the string on your own. It would be more performant and safer.
## Installation
Add the dependency to your `shard.yml`:
```yaml
dependencies:
object-send:
github: j8r/crystal-object-send
```
## Examples
```cr
"abc".send "chars" #=> ['a', 'b', 'c']
"abc".send "lchop('a')" #=> "bc"
"abc".send "insert 1, 'z'" #=> "azbc"
2.send("+ 3") #=> 5
var = "first 2"
[0, 1, 3].send(var) #=> [0, 1]
[0, 1, 2].send("[-1]?") #=> eq 2
[0, 1, 2].send("[..]") #=> eq [0, 1, 2]
```
See more in the [specs](spec/object_send_spec.cr)
## License
Copyright (c) 2019 Julien Reichardt - ISC License