This workshop is important because:
In order to write Ruby code, you're going to need some practice writing Ruby methods. Once you can divide your Ruby code into methods, you'll have greater mastery over your project.
After this workshop, developers will be able to:
- Write conditionals, loops, and methods in Ruby
- Apply methods in ruby to solve problems
- Explain the two main differences between Ruby methods and JavaScript functions: isolated scope and implicit return.
What are the primitive data types in JavaScript? Contrast them with the primitive types in Ruby.
- `null`, `undefined`
- Strings
- Booleans
- Number : `.toString()`
- Arrays : `[index]` to access elements, `indexOf()`,`splice()`, `slice()`, `length`
- Objects: `["some_key"]`, `.some_key`
- `nil`
- Integers: Fixnum, Bignum, `to_s`
- Floats
- Strings: `.to_i` and `.to_f` `*INTEGER`
- Symbols
- Booleans
- Arrays / Ranges : `[x..y]`, `[x...y]`, `index`
- Hashes
- `{ :key => value }`
- `{ key: value }` which is the same as `{:key =>value }`
- `[some_key]` and `[some_key]=`
- `key`,`.keys`, `.each`
Identify the operators in Javascript and Ruby and find the contrasts.
- `=`, `+=`, `*=`, ...
- `==`, `===`, `>`, `>=`, ...
- `!`, `||`, `&&`
- `+`, `-`, `/`, `*`
- `=`, `+=`, `*=`, ...
- `==`, `.equal?`, `>`, `>=`, ...
- `!`, `not`, `||`, `&&`
- `** `, `+`, `-`, `/`, `*`
Create a demo of two conditionals and one loop/iterator:
- Conditionals
if
,elsif
,else
,unless
,case when else
...
- Loops/iterators
until
,while
,times
....each
,for ... in
- block - chunks of code between braces or between do..end:
- used with
.each
,.map
- used with
```ruby
[1, 2, 3].each do |n|
puts "Number #{n}"
end
```
^ is the same as
```ruby
[1, 2, 3].each {|n| puts "Number #{n}"}
```
- use
def
when defining a method. - Methods implicitly returns last evaluation.
- Ruby is locally scoped.
# announce you are creating a method with 'def'
def say_hello
# all logic and action within belongs to the method
puts "Hello"
# end your method definition with 'end'
end
# call the method 'say_hello'
say_hello
def say(something)
puts something
end
say('hello')
say 'hello'
Ruby allows us to specify parameters omitting parentheses:
# calling method, not using parentheses
results = method_name parameter1
If you want to chain another method off the result of your method call, you do need parentheses as you can see below:
# You need to use parentheses if you want to work with the result immediately.
# e.g., if a method returns an array and we want to reverse element order:
results = method_name(parameter1).reverse
def say(something = "Hello")
puts something
end
say # prints "Hello"
say "Goodbye" # prints "Goodbye"
def recurse(depth)
if depth > 0
puts "Spiraling down..."
recurse(depth - 1)
puts "Spiraling up..."
else
puts "Bottom of the rabbit hole"
end
end
recurse(5)
recurse 5
def add_numbers(first, second)
puts first + second
end
add_numbers(1,2)
add_numbers 1, 2
Prompt: What is the difference between printing and returning?
The following method returns a value, but doesn't print anything.
def add_numbers_quietly(first, second)
first + second
end
add_numbers_quietly(1,2)
add_numbers_quietly 1, 2
def implicitly_return_5
if true
5
end
end
implicitly_return_5
Prompt: Write some Ruby that demonstrates the scoping of variables.
The following code wont work. Why?
foo = 1
def do_stuff
foo += 1
bar = 1
puts foo
puts bar
end
do_stuff
puts foo
puts bar
The problem is the ruby is locally scoped. Meaning that a function only has access to its variables and the variables it defined inside of itself.
foo = 1
def do_stuff
foo = 1
foo += 1
bar = 1
puts foo
puts bar
end
do_stuff
puts foo
puts bar
def do_stuff2(x)
foo = x
foo += 1
bar = 1
puts foo
puts bar
end
puts do_stuff2(foo)
Please complete these exercises.