@Petros’s answer is (still) correct, however I got annoyed enough with this that I’ve created a tool to automate the copying and keep your readme synchronised with the source – https://github.com/zakhenry/embedme
Example usage from the accepted answer
Insert a comment into the code fence
```rb
# path/to/your/file.rb
```
Run npx embedme README.md (this assumes you have Nodejs installed)
The README will be written in-place, with the content of the file following your comment
```rb
# path/to/your/file.rb
class MyClass
end
```
Note that as the comment is still there, you can happily re-run npx embedme README.md again and it won’t make any change.
If you have CI, you can also run npx embedme --verify README.md which will return 1 if it detects a diff (this would mean that the source was updated without the readme being updated).