coder1991 coder1991 - 1 month ago 4x
Ruby Question

How to write a Ruby program using only "a-z", "A-Z", ".", "\n" and " "

I am trying to resolve some Ruby challenge and I have big problem with some low level (I suppose?) digits characters conversion.

Just imagine, that you need to write program, that prints on your screen sentence ex.:

"Jon Doe was born in 2017!"

using only following characters [a-zA-Z.\n ] (small & big letters, dot, space and new line)

In fact, I have no idea how should I even start to look for the answer.

Is it some kind of using
method? or is there any trival solution that I can't find?

There is a reason why "write program" was bolded. Question is, what is the simplest definition of program in Ruby?


Writing a program that prints a string using only letters, dot, space and newline might seem impossible at first, but it is actually not that hard.

Lowercase and uppercase letters allow you to invoke Kernel methods (like print and puts) as well as keywords like nil, false and true. A dot allows you to invoke methods with an explicit receiver. Space allows you to pass an argument to a method. Newline separates commands.

Let's try to get an "a":

false                            #=> false
false.inspect                    #=> "false"
false.inspect.chars              #=> ["f", "a", "l", "s", "e"]
false.inspect.chars.rotate       #=> ["a", "l", "s", "e", "f"]
false.inspect.chars.rotate.first #=> "a"

Now lets print "abc":

print false.inspect.chars.rotate.first
print false.inspect.chars.rotate.first.succ
print false.inspect.chars.rotate.first.succ.succ



You get the idea.

And yes, it's also possible to print spaces, punctuation and numbers using a similar approach. But I leave that to you. Take a look at the available methods and be creative.

Additional points for figuring out how to print a string without using space, just [a-zA-Z.\n].