methods
# Metadata
2022-06-01 22:28 | methods | Doriel Rivalet
# Content
implicit return
|
|
|
|
|
|
Chaining methods together like this effectively has each method call build off of the outcome of the previous method in the chain. The process that takes place essentially produces the following steps:
|
|
predicate methods: returns true/false
You can also create your own method with a ?
at the end of its name to indicate that it returns a Boolean.
A general rule in programming is that you do not want your methods to overwrite the objects that you call them on.
By adding a !
to the end of your method, you indicate that this method performs its action and simultaneously overwrites the value of the original object with the result.
|
|
Writing whisper.downcase!
is the equivalent of writing whisper = whisper.downcase
.
procedure is also a name for a method
method definitions create their own scope that’s entirely outside of the execution flow.
When this program starts running, the call stack initially has one item – called a stack frame – that represents the global (top-level) portion of the program. The initial stack frame is sometimes called the main
method. Ruby uses this frame to keep track of what part of the main program it is currently working on.
same as c++ int main()
push and pop the call stack.
Variables name things, methods name behaviour (code).
If we don’t do anything else, then a method will return the return value of the last evaluated statement.
In our example method …
|
|
… the last evaluated statement is the expression number + 2
. Since in our example number
is assigned 3
this expression returns the number 5
, and that is why the value returned by our method also is 5
.
u cant reference something outside of the scope u are in
Use :: for describing class methods, # for describing instance methods, and use . for example code.
# = “octothorpes”.
# Sources
Own notes
https://www.theodinproject.com/lessons/ruby-methods
https://launchschool.com/books/ruby/read/methods
http://ruby-for-beginners.rubymonstas.org/writing_methods.html
# Content Lists
If you prefer browsing the contents of this site through a list instead of a graph, you can find content lists here too: