Skip to content

Commit

Permalink
created doc (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakenetz authored Jul 16, 2024
1 parent 1d41df9 commit 0b9fd3a
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"codeacademy",
"desaturated",
"dweb",
"frontmatter",
Expand Down
137 changes: 137 additions & 0 deletions app/blog/ruby-codeacademy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
meta:
- title: BN | Blog | Ruby Codeacademy
- name: description
content: Reflections on completing the "Learn Ruby" Codeacademy course

attributes:
date: Jul 16, 2024
---

export const meta = frontmatter.meta;
export const headers = frontmatter.headers;
export const attributes = frontmatter.attributes;

# Reflections on Completing the "Learn Ruby" Codeacademy Course

In an effort to throw another tool in my technical toolbox, I completed
[Codeacademy's "Learn Ruby" course](https://www.codecademy.com/courses/learn-ruby).
I was enticed to use Codeacademy's platform because of a
[Software Engineering Daily podcast](https://softwareengineeringdaily.com/2024/06/18/16909/?utm_source=rss&utm_medium=rss&utm_campaign=16909)
featuring Codeacademy's Senior Curriculum Director Zoe Bachman. In said podcast,
Bachman mentioned courses designed to cater to specific audience, which
unfortunately for me, wasn't the case with the Ruby offerings. womp womp.
Regardless, As a primarily JS/TS developer, I thought I'd document some of my
musings about Ruby.

## Javascript vs Ruby

I'm not sure if this is heresy or not, but in all honestly, JS and Ruby aren't
wildly that different. To me, the most noticeable difference was the omission of
braces, but once I mentally adjusted, the language felt oddly familiar. Both are
[high-level languages](https://en.wikipedia.org/wiki/High-level_programming_language)
that are
[dynamically typed](https://developer.mozilla.org/en-US/docs/Glossary/Dynamic_typing)
with a
[JIT compiler](https://www.freecodecamp.org/news/just-in-time-compilation-explained/)
and
[garbage collection](<https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)>).
They share a significant amount of keywords and operands, and they support
multiple paradigms, such as OOP, procedural, etc.

From what I've read, Ruby has a commitment to english-like syntax. For example,
you can syntactically inverse an `if` statement, which I'm presuming makes it
feel more native:

```console
<statement> if <condition>
if <condition> do <statement>
```

Or reduce the gobbledygook of an iterator:

```console
5.times do <statement>
```

### Functions, etc

The one area I was repeatedly tripped-up with was with `functions`. For one,
Ruby has an implicit return value. I suppose this is to encourage functional
programming and limit side-effects; thought don't quote me on that.

```console
def greeting(name)
return "Hello, #{name}!"
end

def same_greeting_different_syntax(name)
"Hello, #{name}!"
end
```

And then there's the proc, lambda, block thing. Let's start with `blocks`. From
what I gather, a block is a function that can be declared 2 different ways:

```console
do |n|
puts n
end

{ |n| puts n }
```

Either way, the parameters are defined between `|pipes|`. The general syntax I
kept seeing was: an iterator is called and then followed by a block:

```console
2.times do |i|
puts i
end

[1,2,3].each { |v| puts v }
```

Now the `lambda` v `proc` thing gets a little more nuanced and I'm not entirely
certain on how it came about. First we'll start with the signatures:

```console
proc_test = Proc.new { puts "I am the proc method!" }
lambda_test = lambda { puts "I am the lambda method!"}

proc_test.call # => I am the proc method!
lambda_test.call # => I am the lambda method!
```

Ok, we see the block syntax we called out above and it returns an object with a
`call` method to it. I'm guessing this is because in Ruby (as in JS)
["everything is an object"](https://ruby-doc.org/core-2.5.1/Object.html) or at
least has a
[object wrapper](https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object/9110389#9110389).

The nuance occurs when returning. `lambda` return from its own context, while a
`proc` returns out of the embracing method's context. So does that mean a `proc`
has closure over it's calling method? That I'm not sure. Let's see the examples
though:

```console
def proc_demo_method
proc_demo = Proc.new { return "Only I print!" }
proc_demo.call
"But what about me?" # Never reached
end

puts proc_demo_method # Only I print!

def lambda_demo_method
lambda_demo = lambda { return "Will I print?" }
lambda_demo.call
"Sorry - it's me that's printed."
end

puts lambda_demo_method # Sorry - it's me that's printed.
```

For now, that's that. I might go back and update this when I'm a little more
educated. But in the meantime, let me know what you think by emailing me at
**[email protected]**

0 comments on commit 0b9fd3a

Please sign in to comment.