Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'executing ruby' and minor fixes
- Loading branch information
Fabian Pfaff
committed
Nov 5, 2016
1 parent
1bf08b8
commit 0022055
Showing
8 changed files
with
79 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,52 @@ | ||
== Ruby | ||
|
||
A dynamic, reflective, object-oriented, general-purpose programming language. | ||
Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. | ||
|
||
=== "Hello, World!" Ruby program | ||
|
||
[source, ruby] | ||
---- | ||
include::res/hello_world/hello.rb[] | ||
---- | ||
|
||
=== Executing Ruby source files | ||
|
||
Ruby source files are by convention marked with the `.rb` extension. | ||
To execute the hello world program you have to save it into a file, e.g. `hello.rb`. | ||
You can then run it from the command line. | ||
|
||
[source, terminal] | ||
---- | ||
include::res/terminal/hello[] | ||
---- | ||
|
||
If you want to omit the `ruby` before your file name you have to add a shebang line as the first line in your file. | ||
|
||
[source, ruby] | ||
---- | ||
include::res/shebang/shebang_line[] | ||
---- | ||
|
||
If you are using a Unix-like operating system you also have to make the file executable with `chmod`. | ||
|
||
[source, terminal] | ||
---- | ||
include::res/terminal/chmod[] | ||
---- | ||
|
||
=== The Ruby console | ||
The Ruby console offers a quick way to execute Ruby code. It allows you to type in Ruby commands and evaluates them. | ||
You can call it from your terminal by issuing the command `irb`. Type in our hello world program: | ||
|
||
[source, ruby] | ||
---- | ||
include::res/hello_world/hello.rb[] | ||
---- | ||
Press enter-> and the code will be executed. | ||
|
||
You can even prototype methods and classes inside the console. | ||
|
||
[source, ruby] | ||
---- | ||
include::res/irb/method_prototyping[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
puts 'Hello, World!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
irb(main):001:0> def reverse(ary) | ||
irb(main):002:1> ary.reduce([]) { |a, e| a.unshift(e) } | ||
irb(main):003:1> end | ||
=> :reverse | ||
irb(main):004:0> reverse([1, 2, 3, 4]) | ||
=> [4, 3, 2, 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#!/usr/bin/ruby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
chmod +x hello.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ruby hello.rb | ||
Hello, World! |