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
| def better_acronymise(string) | |
| return string.split.map { |word| word[0].upcase}.join | |
| end | |
| p better_acronymise("Frequently Asked Question") | |
| puts better_acronymise("Frequently Asked Question") == "FAQ" | |
| p better_acronymise("Bois d'Arcy") | |
| puts better_acronymise("Bois d'Arcy") == "BD" | |
| p better_acronymise(" Hello World ") | |
| puts better_acronymise(" Hello World ") == "HW" |
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
| def encrypt(text) | |
| alphabet = ("A".."Z").to_a | |
| text.split(' ').map do |word| | |
| word.split('').map do |letter| | |
| position = alphabet.index(letter) | |
| new_position = position - 3 | |
| alphabet[new_position] | |
| end.join('') | |
| end.join(' ') |
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
| def beautify_name(first_name, last_name) | |
| full_name = "#{first_name.capitalize} #{last_name.upcase}" | |
| yield(full_name) | |
| end | |
| message = beautify_name("john", "lennon") do |full_name| | |
| "Greetings #{full_name}, you look quite fine today!" | |
| end | |
| puts message # => "Greetings John LENNON, you look quite fine today!" |
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
| def something_slow | |
| time = Time.now | |
| puts "I am doing something slow" | |
| yield | |
| puts "Elapsed time : #{Time.now - time}" | |
| end | |
| something_slow { sleep(2) } | |
| something_slow { sleep(1) } |
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
| musicians = [ | |
| 'Jimmy Page', | |
| 'Robert Plant', | |
| 'John Paul Jones', | |
| 'John Bonham' | |
| ] | |
| # SELECT | |
| begins_with_r = musicians.select { |musician| musician.start_with?("R") } |
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
| def acronymise(string) | |
| # look for first letter in each word | |
| # concatenate in a string | |
| # upcase | |
| # return value | |
| acronym = "" | |
| string.split(" ").each do |word| | |
| acronym += word[0].upcase | |
| end | |
| return acronym |
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
| def game_refacto | |
| game_logic = { 'rock' => 'scissors', 'paper' => 'rock', 'scissors' => 'paper' } | |
| options = game_logic.keys | |
| computer_input = options.sample | |
| puts "#{options.join(', ')}?" | |
| player_input = gets.chomp.downcase | |
| win = "You win! Computer input was #{computer_input}" | |
| lose = "You lose! Computer input was #{computer_input}" |
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
| def game | |
| # create an array [rock, paper, scissors] | |
| options = ["rock", "paper", "scissors"] | |
| # create a random input | |
| computer_input = options.sample | |
| puts "Rock, paper, scissors?" | |
| player_input = gets.chomp.downcase | |
| # create player's input |
NewerOlder