Skip to content

Instantly share code, notes, and snippets.

@Nicolab
Created January 17, 2020 13:43
Show Gist options
  • Save Nicolab/5dd359655ae585df518644ef1e0700bc to your computer and use it in GitHub Desktop.
Save Nicolab/5dd359655ae585df518644ef1e0700bc to your computer and use it in GitHub Desktop.
Multi-line eval for Crystal lang. Write multi-line Crystal code in the stdin / terminal and eval it.

Eval multi-line code in stdin (terminal or SHELL script):

You can just write to crystal eval's stdin if you don't provide any arguments. Or no source argument, to be precise.

Just close stdin to eval it. Depends on you terminal, but usually Ctrl+D.

Another way:

# open a repl like
crystal eval <<__cr

Write the code (hit the enter touch of the keyboard to make a next line)

> puts 1 # [enter]
> puts 2 # [enter]
> puts "Welcome to 'the poor man's repl'" # [enter]
> __cr # <- close the sequence [enter]

To use in the terminal (stdin) with a command, add an alias to .bashrc:

# Multi-line eval for Crystal lang. 
# Write multi-line Crystal code in the stdin / terminal and eval it.
# Finish the sequence with a new line beginning by `:e`
alias cre='crystal eval <<:e'

As a SHELL (~/bin/cre):

#!/bin/bash

# Multi-line eval for Crystal lang. 
# Write multi-line Crystal code in the stdin / terminal and eval it.
# Finish the sequence with a new line beginning by `:e`
crystal eval <<:e

Change the permissions of the file to make this script executable:

chmod +x ~/bin/cre

Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment