Your First Program
5 min · Ruby
Write and run your very first Ruby program, right here on the page.
Enemy 1 of 5
Ruby keeps things short
Ruby was designed with a specific goal in mind: make the language feel natural to write and pleasant to read, even for beginners. You'll notice that right away. There's no boilerplate to type before you can print something. puts (short for "put string") is the everyday way to write a line of output, and it's a method you call directly, with no special ceremony around it.
puts does one extra thing that's easy to overlook at first: it automatically adds a newline character after whatever you give it. That's actually where the name half comes from. Think of it as "puts this line, and a break at the end." This is different from some other languages' basic print functions, which print exactly what you give them and nothing more, leaving you to add the line break yourself.
The text goes inside quotes, either double ("...") or single ('...'), which tell Ruby where the string starts and ends. Whatever is between the quotes prints exactly as written, aside from a few special escape sequences in double-quoted strings.
Ruby doesn't require a semicolon at the end of a line the way PHP or JavaScript-family languages do. A line break is enough to end a statement. You can put a semicolon there if you want to squeeze two statements onto one line, but it's optional and rarely used for single statements.
Loading editor...