Forum: Ruby Formatting Ruby Text?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C71c441dcbc412a9bbafdc53262286fe?d=identicon&s=25 Carter Davis (jearb)
on 2008-11-09 21:54
Hello. I was wondering...Is it possible to format text in Ruby besides
using the string literals like \n or \t? For example, can you make text
bold?
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2008-11-09 23:57
Carter Davis wrote:
> Hello. I was wondering...Is it possible to format text in Ruby besides
> using the string literals like \n or \t? For example, can you make text
> bold?

Not sure I understand your question correctly. If you mean the
source-code then you're looking for a syntax highlighting editor which
doesn't touch the text at all but just highlights it.
If you mean when outputting e.g. via puts then it entirely depends on
your output device. If it is a terminal that supports ANSII escape
sequences then you can use them. Look for the Term::ANSIColor gem on
rubyforge for a convenient way to do that. Plain it is like this:
puts "\e[1mBold\e[0m"
puts "\e[31mRed\e[0m"

With the gem you can do it like this:
puts "Bold".bold
puts "Red".red
(iirc)

Regards
Stefan
This topic is locked and can not be replied to.