Created
July 28, 2020 08:59
-
-
Save rodloboz/d279f9aa35e6e6a0771aa89d36377d04 to your computer and use it in GitHub Desktop.
01-Ruby/01-Programming Basics
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
| require 'date' | |
| # A method that receives two integers | |
| # and returns the one that's greater | |
| def max(x, y) | |
| # compare x with y | |
| # if x is greater | |
| if x > y | |
| # return x | |
| return x | |
| # otherwise | |
| else | |
| # return y | |
| return y | |
| end | |
| end | |
| puts max(3, 5) | |
| puts max(909094534, 32312) | |
| first_number = 7 | |
| second_number = 3 | |
| puts max(first_number, second_number) | |
| def tomorrow | |
| tomorrow_date = Date.today + 1 | |
| end | |
| puts tomorrow | |
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
| # "Good morning".class => String | |
| # true.class => TrueClass (Boolean) | |
| # false.class => FalseClass (Boolean) | |
| # 5.class => Integer | |
| # 3.14 => Float | |
| # ["Hello", 3, true, 3.14].class => Array | |
| # (1..10).class | |
| # nil.class => NilClass | |
| # String Interpolation | |
| # must use DOUBLE QUOTES | |
| # "Hello, #{'world'.upcase}" | |
| # String concatenation | |
| # 'Hello' + ' ' + 'World' => 'Hello World' | |
| puts "Hello, World!" | |
| # Array of Strings | |
| # %w[Zoe Susan John Alex] |
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
| # Defining methods | |
| # Methods are like recipes | |
| # They are a set of instructions | |
| # def say_hello | |
| # return "Hello, world!" | |
| # end # => Returns nil (because of puts) | |
| # Let's prepare the 'meal' | |
| # puts say_hello # call the method | |
| # "Hello, Rui" | |
| # Method Arguments are like recipe ingredients | |
| def say_hello(name) | |
| "Hello, #{name}" | |
| end | |
| # "John" is a parameter | |
| # that will be captured | |
| # inside the methed in name | |
| name = "Mike" | |
| puts say_hello("John") | |
| puts say_hello("Gus") | |
| puts say_hello(name) | |
| puts "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
| age = 17 # declaring and assigning | |
| # String concatenation | |
| # puts "The value of age is: " + age.to_s | |
| # String Interpolation | |
| puts "The value of age is: #{age}" | |
| puts "It's your birthday today!" | |
| # 17 + 1 => 18 | |
| age = age + 1 # Reassigning | |
| puts "Age is now: #{age}" | |
| # shorthand syntax | |
| age += 1 # equivalent to age = age + 1 | |
| puts "Age is now: #{age}" | |
| # firstName => Camel case (JavaScript) | |
| # FirstName => Upper Camel case (Class names) | |
| # first_name => snake_case (variables, method names and filenames) | |
| first_name = "rui" | |
| puts "My name is #{first_name.capitalize}" | |
| names = ["John", "Susan", "Abe", "Michael"] | |
| puts names.class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment