Skip to content

Commit

Permalink
Added 'executing ruby' and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Pfaff committed Nov 5, 2016
1 parent 1bf08b8 commit 0022055
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 21 deletions.
2 changes: 0 additions & 2 deletions Ruby/001_article.adoc
Expand Up @@ -8,8 +8,6 @@ Fabian Pfaff (c) 2016 vogella GmbH
Version 0.1, 01.11.2016
:docinfodir: ../
:vgwort: Ruby

Some notes about Ruby

include::010_overview.adoc[]
include::020_installation.adoc[]
Expand Down
50 changes: 49 additions & 1 deletion Ruby/010_overview.adoc
@@ -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[]
----
37 changes: 19 additions & 18 deletions Ruby/030_debugging.adoc
Expand Up @@ -33,7 +33,7 @@ pp ary
=> [1, 2, [3, 4]]
----

Some useful methods to use in conjunction with puts are
Some useful methods to use in conjunction with 'puts' are

* instance_variables: all instance variables of the object
* methods: all available methods of the class instance
Expand Down Expand Up @@ -82,7 +82,7 @@ a
[TIP]
====
If your program makes use of a framework like Rails or Sinatra your stacktrace might be quite convoluted.
It can help to filter the stack trace for your project name.
It might be useful to filter the stack trace by your project name.
[source, ruby]
----
puts caller.select { |line| line.include? 'project_name' }
Expand All @@ -98,9 +98,10 @@ puts caller.select { |line| line['project_name'] }
=== Debugging with pry-byebug
Interactive step-by-step debugging can be done with pry-byebug, which adds debugging and stack navigation to the ruby console pry.
To use it you have to add it as a dependency to your project.
If your project uses bundler you add it to your Gemfile.
If your project uses bundler must you add it to your Gemfile.
// TODO(fap): should move to general gem installation guide?

[source, ruby]
----
group :development do
# your other dev dependencies
Expand Down Expand Up @@ -139,33 +140,33 @@ Once you have entered pry you can execute statements like you would normally do

[source, ruby]
----
2: def factorial(n, acc = 1)
3: binding.pry
=> 4: return acc if n <= 1
5: fact(n - 1, n * acc)
6: end
2: def factorial(n, acc = 1)
3: binding.pry
=> 4: return acc if n <= 1
5: fact(n - 1, n * acc)
6: end
pry(#<Math>)> n
=> 4
pry(#<Math>)> acc
=> 1
pry(#<Math>)> next
2: def factorial(n, acc = 1)
3: binding.pry
4: return acc if n <= 1
=> 5: fact(n - 1, n * acc)
6: end
2: def factorial(n, acc = 1)
3: binding.pry
4: return acc if n <= 1
=> 5: fact(n - 1, n * acc)
6: end
pry(#<Math>)> n
=> 4
pry(#<Math>)> continue
2: def factorial(n, acc = 1)
3: binding.pry
=> 4: return acc if n <= 1
5: fact(n - 1, n * acc)
6: end
2: def factorial(n, acc = 1)
3: binding.pry
=> 4: return acc if n <= 1
5: fact(n - 1, n * acc)
6: end
pry(#<Math>)> n
=> 3
Expand Down
1 change: 1 addition & 0 deletions Ruby/res/hello_world/hello.rb
@@ -0,0 +1 @@
puts 'Hello, World!'
6 changes: 6 additions & 0 deletions Ruby/res/irb/method_prototyping
@@ -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]
1 change: 1 addition & 0 deletions Ruby/res/shebang/shebang_line
@@ -0,0 +1 @@
#!/usr/bin/ruby
1 change: 1 addition & 0 deletions Ruby/res/terminal/chmod
@@ -0,0 +1 @@
chmod +x hello.rb
2 changes: 2 additions & 0 deletions Ruby/res/terminal/hello
@@ -0,0 +1,2 @@
ruby hello.rb
Hello, World!

0 comments on commit 0022055

Please sign in to comment.