||=
a || = b
is a conditional operator. Assign b to a if a is uninitialized.
||=
a || = b
is a conditional operator. Assign b to a if a is uninitialized.
Keyboard shortcuts βββββββββββββββ
Show suggestion - ctrl + space
Shifting between terminal and window: Terminal ctrl ` , Window ctrl 1
Open keyboard shortcuts : cmd K + cmd S
| # Ruby Yield Statement | |
| # Yield statement is used for a method that takes in a block and needs to invoke the code block. | |
| # In this e.g, name() method takes in a block defined in line #10. | |
| # In the method definition, wherever 'yield' keyword comes up, the code block is rendered. | |
| def name | |
| puts "A yield will be called with id of 12" | |
| yield 12 |
| # with this syntax, the order of parameters does not matter | |
| # have to use same name for parameters when passing arguments | |
| #! required and optional arguments are ordered | |
| #! order of passing parameters | |
| #! required => optional => variable => keyword | |
| #! keyword arguments => for more clarity, and removing the need for arguments to be ordered | |
| def method_1(m1:, m2:, m3:) |
| # single splat operator converts all arguments into an array. | |
| # double splat operator converts all arguments into a hash | |
| # order of arguments: | |
| #// required -> optional -> variable -> keyword (use hash syntax) | |
| def dubSplat (*c, **d) | |
| p c | |
| p d | |
| end |
| #// RAILS ASSOCIATIONS | |
| class Author | |
| has_one :book | |
| # indicates that one other model has a reference to this model. | |
| # Rails assumes that Books.author_id is a foreign key to Author.id in this table. | |
| end | |
| class Book < AR | |
| belongs_to :author |