Hello World #
You can find all the code for this chapter here
It is traditional for your first program in a new language to be Hello, World.
- Create a folder wherever you like
- Put a new file in it called
hello.rb
and put the following code inside it
def main
print("Hello, world")
end
main
To run it type ruby hello.rb
.
How it works #
When you write a program in Ruby just can define a method and after that just call it.
The def
keyword is how you define a function with a name and a body.
Ruby provide out of the box a function called print
that we use to print out to the console.
How to test #
How do you test this? It is good to separate your “domain” code from the outside world (side-effects).
The print
is a side effect (printing to stdout) and the string we send in is our domain.
So let’s separate these concerns so it’s easier to test
def hello
return "Hello, world"
end
def main
print(hello)
end
main
We have created a new function again with def
called hello.
Now create a new file called hello_test.rb
where we are going to write a test for our hello
function
require "minitest/autorun"
require_relative "hello"
class TestHello < Minitest::Test
def test_hello
assert_equal hello, "Hello, world"
end
end