Skip to content

Instantly share code, notes, and snippets.

View RickArora's full-sized avatar

Ricky RickArora

View GitHub Profile
# Return the argument with all its lowercase characters removed.
def destructive_uppercase(str)
var x
str.each_char do { |el|
if el === [A..Z]
el += x }
end
return x
def destructive_uppercase(str)
x = ""
str.each_char do |el|
if el === [A..Z]
el += x
end
return x
def destructive_uppercase(str)
x = ""
str.chars.each do |el|
if el == [A..Z]
el += x
end
return x
def devowel(str)
vowels = ["a", "e", "i", "o", "u"]
new_str = ""
# We turn the string into an array of characters using chars.
# An alternative to this is the method each_char (covered below!)
str.chars.each do |ch|
next if vowels.include?(ch.downcase)
# the code below is only reachable when ch is a consonant
new_str += ch
def destructive_uppercase(str)
x = ""
str.chars.each do |el|
if el == [A..Z]
el += x
end
end
return x
def destructive_uppercase(str)
x = ""
str.chars.each do |el|
if el === [A..Z]
x += el
end
end
end
return x
# Return the middle character of a string. Return the middle two characters if
# the word is of even length, e.g. middle_substring("middle") => "dd",
# middle_substring("mid") => "i"
def middle_substring(str)
finalString = ""
if (str.length % 2) == 0
x = str.chars
finalString += x[(x.(length/2).ceil]
finalString += x[x.(length/2).floor]
# Return the middle character of a string. Return the middle two characters if
# the word is of even length, e.g. middle_substring("middle") => "dd",
# middle_substring("mid") => "i"
def middle_substring(str)
finalString = ""
if (str.length % 2) == 0
x = str.chars
finalString += x[((x.length)/2).ceil]
finalString += x[((x.length)/2).floor]
# Write a method that returns a boolean indicating whether the argument is
# prime.
def prime?(num)
(1..num).each do |i|
if (num % i == 0 && i != 1 && i != num) || (num == 1)
return false
end
end
return true;
end
# Write a method that returns a boolean indicating whether the argument is
# prime.
def prime?(num)
(1..num).each do |i|
if (num % i == 0 && i != 1 && i != num) || (num == 1)
return false
end
end
return true;