I am trying to write a function in the rails console, and in the example, this is how the function should look in terminal.
>> def string_message(str = '')
>> return "It's an empty string!" if str.empty?
>> return "The string is nonempty."
>> end
How do they create a new line while still making the console realize it is all of the lines create a function. Would it be accurate to write it as:
>> def string_message(str = '') \n\t return "It's an empty string!" if str.empty? \n\t blah blah \n\t
?
IRB, the ruby console in which
rails console
relies supports this out of the box.Just type your function declaration, press the enter key, then input the body line by line, and finally type
end
.You'll see text like this:
Notice how the
?
indicates that IRB is waiting for more input before evaluating the expression.