mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Elixir agents/maps, Ruby conventions/docs (#2116)
* Add name to contributors list * Fix typo * Note convention on curly-braced blocks * Replace hardcoded link to 2.1.1 documentation with generic current link * Add notes on Elixir agents * Add explanation of maps * Add name to contributors list * Fix code fence that was obscuring markdown * Fix syntax error * Remove disputed comment * Remove from contributors list
This commit is contained in:
parent
b16c7ee2d8
commit
e249d12ee6
@ -3,6 +3,7 @@ language: elixir
|
||||
contributors:
|
||||
- ["Joao Marques", "http://github.com/mrshankly"]
|
||||
- ["Dzianis Dashkevich", "https://github.com/dskecse"]
|
||||
- ["Ryan Plant", "https://github.com/ryanplant-au"]
|
||||
filename: learnelixir.ex
|
||||
---
|
||||
|
||||
@ -96,6 +97,14 @@ string.
|
||||
lower..upper = 1..10 # Can use pattern matching on ranges as well
|
||||
[lower, upper] #=> [1, 10]
|
||||
|
||||
# Maps are key-value pairs
|
||||
genders = %{"david" => "male", "gillian" => "female"}
|
||||
genders["david"] #=> "male"
|
||||
|
||||
# Maps with atom keys can be used like this
|
||||
genders = %{david: "male", gillian: "female"}
|
||||
genders.gillian #=> "female"
|
||||
|
||||
## ---------------------------
|
||||
## -- Operators
|
||||
## ---------------------------
|
||||
@ -407,6 +416,23 @@ send pid, {:circle, 2}
|
||||
|
||||
# The shell is also a process, you can use `self` to get the current pid
|
||||
self() #=> #PID<0.27.0>
|
||||
|
||||
## ---------------------------
|
||||
## -- Agents
|
||||
## ---------------------------
|
||||
|
||||
# An agent is a process that keeps track of some changing value
|
||||
|
||||
# Create an agent with `Agent.start_link`, passing in a function
|
||||
# The initial state of the agent will be whatever that function returns
|
||||
{ok, my_agent} = Agent.start_link(fn -> ["red, green"] end)
|
||||
|
||||
# `Agent.get` takes an agent name and a `fn` that gets passed the current state
|
||||
# Whatever that `fn` returns is what you'll get back
|
||||
Agent.get(my_agent, fn colors -> colors end) #=> ["red, "green"]
|
||||
|
||||
# Update the agent's state the same way
|
||||
Agent.update(my_agent, fn colors -> ["blue" | colors] end)
|
||||
```
|
||||
|
||||
## References
|
||||
|
@ -210,7 +210,7 @@ array.push(6) #=> [1, 2, 3, 4, 5, 6]
|
||||
# Check if an item exists in an array
|
||||
array.include?(1) #=> true
|
||||
|
||||
# Hashes are Ruby's primary dictionary with keys/value pairs.
|
||||
# Hashes are Ruby's primary dictionary with key/value pairs.
|
||||
# Hashes are denoted with curly braces:
|
||||
hash = { 'color' => 'green', 'number' => 5 }
|
||||
|
||||
@ -612,7 +612,7 @@ Something.new.qux # => 'qux'
|
||||
|
||||
- [Learn Ruby by Example with Challenges](http://www.learneroo.com/modules/61/nodes/338) - A variant of this reference with in-browser challenges.
|
||||
- [An Interactive Tutorial for Ruby](https://rubymonk.com/) - Learn Ruby through a series of interactive tutorials.
|
||||
- [Official Documentation](http://www.ruby-doc.org/core-2.1.1/)
|
||||
- [Official Documentation](http://ruby-doc.org/core)
|
||||
- [Ruby from other languages](https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/)
|
||||
- [Programming Ruby](http://www.amazon.com/Programming-Ruby-1-9-2-0-Programmers/dp/1937785491/) - An older [free edition](http://ruby-doc.com/docs/ProgrammingRuby/) is available online.
|
||||
- [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) - A community-driven Ruby coding style guide.
|
||||
|
Loading…
Reference in New Issue
Block a user