This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Book | |
| attr_accessor :title, :isbn, :author, :price | |
| def initialize(title, isbn, author, price) | |
| @title = title | |
| @isbn = isbn | |
| @author = author | |
| @price = price | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Book | |
| attr_accessor :title, :isbn, :author, :price | |
| def initialize(title, isbn, author, price) | |
| @title = title | |
| @isbn = isbn | |
| @author = author | |
| @price = price | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Book | |
| # A comment | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FictionBook # camel case for class names | |
| #... | |
| end | |
| def days_since_launch # snakes everywhere else | |
| #... | |
| end | |
| STANDARD_PRICE = 10 # uppercase camel case for constants |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Parentheses are optional but usually preferred when defining or calling methods with parameters | |
| def find_book(isbn) | |
| # ... | |
| end | |
| find_book('978-0321584106') | |
| # Parentheses are left off when methods have no parameters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ["John", "Mark", "Luke"].each { |name| puts name } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ["John", "Mark", "Luke"].each do |name| | |
| puts "What's their name?" | |
| puts "I think it's #{name}." | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if i < 0 | |
| puts "#{i} is a negative number" | |
| elsif i > 0 | |
| puts "#{i} is a positive number" | |
| else | |
| puts "The number is zero" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Instead of if not | |
| if not @private | |
| puts "This product is available." | |
| end | |
| # use unless | |
| unless @private | |
| puts "This product is available." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| i = 1 | |
| while i < 11 | |
| puts i | |
| i += 1 | |
| end |
OlderNewer