This file contains 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
array = [3,7,1,9,2,12,34,0] | |
# sort in ascending order | |
array.sort | |
# => [0, 1, 2, 3, 7, 9, 12, 34] | |
# sort in descending order | |
array.sort.reverse | |
# => [34, 12, 9, 7, 3, 2, 1, 0] |
This file contains 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
a1 = "A Test" | |
a2 = "A Boom" | |
a1 <=> a2 | |
# => 1 | |
b1 = "2" | |
b2 = "9" | |
b1 <=> b2 |
This file contains 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 greet message, *people | |
people.each { |p| puts "#{message}, #{p}!" } | |
end | |
greet "Hello", "Tony", "Ann", "BadMan" |
This file contains 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 english_2_pig_latin string | |
string.split(/ /).each do |word| | |
word << word[0] << "ay " | |
print word[1..word.length] | |
end | |
end |
This file contains 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 list_to_frame list=[] | |
list = ["Hello", "World", "in", "a", "frame"] if list.empty? | |
longest = 0 | |
list.each do |element| | |
longest = element.length if longest < element.length | |
end | |
(0..longest+3).each do |
This file contains 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 sum_or_product | |
print 'Enter any number bigger 1: ' | |
num = gets.to_i | |
print 'sum [s] or product [p]? ' | |
opt = gets.chomp | |
if opt == 's' | |
(1..num).inject :+ | |
elsif opt == 'p' |
This file contains 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 sum_to | |
print 'Enter any number greater than 1: ' | |
input = gets.to_i | |
sum = 0 | |
for a in 1..input do | |
sum += a | |
end | |
This file contains 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 rand_str length | |
('a'..'z').to_a.shuffle[0..length].join | |
end |
This file contains 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
public class Dice { | |
private int anzahl; | |
private int grenze = 6; | |
private int[] zaehler = new int[6]; | |
Dice(int wuerfeAnzahl){ | |
anzahl = wuerfeAnzahl; | |
This file contains 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
public class Arrays { | |
public static void main(String[] args) { | |
int[] numbers = new int[5]; | |
numbers[0] = 5; | |
int l = numbers.length; |