1

I’m using a gem called artii ( http://rubygems.org/gems/artii ) that creates ascii art images from text.

I can only seem to call it using system(), however I’d like to display the result as text in a webpage

My .rb file:

def makeText
  @word = system('artii Hello World')
  puts @word
  @word
end

result of puts:

=>  _    _      _ _       
=> | |  | |    | | |       
=> | |__| | ___| | | ___  
=> |  __  |/ _ \ | |/ _ \ 
=> | |  | |  __/ | | (_) |
=> |_|  |_|\___|_|_|\___/ 

Then, in my haml file:

#{makeText} 
=> true

Is there a way to take the result from the command line and convert it to a string, array, or hash to display in a webpage?

Thanks!

2 Answers 2

2

It seems ridiculous to me to call the gem as external command, either using system or backticks. You can use it from Ruby as a Ruby library, without any system interaction. The simplest invocation would be:

@word = Artii::Base.asciify('Hello World')

If you want more complex invocation (i.e. different fonts, styles, etc), then check out that gem's documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to use backticks rather than the system method. Just enclose your shell command in backticks, and the return value will be a string containing whatever it output to standard out.

@word = `artii Hello World`

Note: Be careful not to pass user input to the shell without sanitizing it first, to prevent malicious users from executing arbitrary shell commands. As long as you're the one supplying the string in backticks, and not the user, you're fine.

3 Comments

Great, thanks! Now I have a string but with the line returns removed - I’ve tried using String.split(“n”) as well as \r\n Any further help appreciated.
Do you want an array of lines? Then the command you're looking for is @word.split("\n"). "\n" is newline, the default string separator (in most cases).
finally got it... had to use word.split(“\\n”) then gsub(/\n/, '<br />’) to insert line breaks for the html, and input.gsub(" ", '&nbsp;’) to take care of whitespace Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.