Last active
December 12, 2016 16:38
-
-
Save gabriel-dehan/5c3f501d69bac6bfe4caa51284cc4a2e to your computer and use it in GitHub Desktop.
Correction Exercices Ruby du Jour 1 - Dawan
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
# Permutation | |
question = "La vie ?" | |
answer = 42 | |
puts question | |
puts answer | |
# Methode 1 | |
temp = question | |
question = answer | |
answer = temp | |
puts question | |
puts answer | |
# Methode 2 | |
question, answer = answer, question | |
puts question | |
puts answer |
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 introduce_me(first_name, last_name, age) | |
# Concatenation | |
# return "Vous etes " + first_name + " " + last_name + ", et vous avez " + age.to_s + " ans !" | |
# Interpolation | |
return "Vous etes #{first_name.capitalize} #{last_name.capitalize}, et vous avez #{age} ans !" | |
end | |
# sentence = introduce_me("Gabriel", "dehan", 26) | |
# puts sentence | |
# DRY = Don't Repeat Yourself | |
=begin | |
puts "Quel est votre prenom ?" | |
print "> " | |
prenom = gets.chomp | |
puts "Quel est votre nom ?" | |
print "> " | |
nom = gets.chomp | |
puts "Quel est votre age ?" | |
print "> " | |
age = gets.chomp | |
puts introduce_me(prenom, nom, age) | |
=end | |
def ask_for(information) | |
puts "Quel est votre #{information} ?" | |
print "> " | |
return gets.chomp | |
end | |
prenom = ask_for("prenom") | |
nom = ask_for("nom") | |
age = ask_for("age").to_i | |
puts introduce_me(prenom, nom, age) |
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
# -*- coding: utf-8 -*- | |
def clean_whitespaces(text) | |
# text.lstrip.rstrip | |
text.strip | |
end | |
def is_in?(sentence, word) | |
return sentence.include?(word) | |
end | |
# puts is_in?("hey jude", "jude") | |
def replace(string, character, replacement) | |
# string.sub(character, replacement) | |
string.gsub(character, replacement) | |
end | |
# puts replace("casanova", "a", "o") # => "cosonovo" | |
def divisible_by_two?(number) | |
number.even? | |
# TODO: Retournez true si `number` est divisible par 2 | |
# Exemple d'appel: divisible_by_two?(6) => true | |
end | |
# puts divisible_by_two?(6) #=> true | |
def randomize(array) | |
array.shuffle | |
# TODO: Retournez `array` mélangé. | |
# Exemple d'appel: randomize([1, 2, 3, 4]) => [2, 1, 4, 3] | |
end | |
# p randomize([1, 2, 3, 4]) # => [2, 1, 4, 3] | |
def random_subset(array, count) | |
array.sample(count) | |
# array.shuffle.take(count) | |
# array.shuffle.slice(0, count) | |
# array.shuffle[0..count] | |
# TODO: Retournez un morceau de `array` mélangé. La taille du tableau retourné doit être de `count` élements. | |
# Exemple d'appel: random_subset(('a'..'z').to_a, 4) => ["u", "q", "l", "t"] | |
end | |
def ascending_order(array) | |
array.sort | |
# TODO: Retournez une copie du tableau `array` trié par ordre ascendant. | |
# Exemple d'appel: ascending_order([5, 3, 1, 6, 9]) => [1, 3, 5, 7, 9] | |
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
def tag(tag_name, content, id) | |
"<#{tag_name} id=\"#{id}\">#{content}</#{tag_name}>" | |
end | |
tag('h1', "Ruby", 'titre') | |
# => <h1 id="titre">Ruby</h1> | |
tag('p', "Ce paragraphe contient du texte", 'paragraph') | |
# => <p id="paragraph">Ce paragraphe contient du texte</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment