Home > katas > Remove String Spaces (8kyu) [Ruby]

Remove String Spaces (8kyu) [Ruby]


Chek kata on Codewars

Description:

Simple, remove the spaces from the string, then return the resultant string.
Solution 1.1

Let's start with a loop solution.

def no_space(x)
    string = ""

    x.chars.each do |char|
        if char != " "
            string += char
        end
    end

    string
end

Solution 2.1

Let's solve it with regular expression.

def no_space(x)
  x.gsub(/ /,"")
end

Solution 3.1

Let's solve it with split and join.

def no_space(x)
  x.split(" ").join
end

Solution 3.2

Let's solve it with filter aka select.

def no_space(x)
    x.chars.select{ |char| char != ' ' }.join
end

Solution 3.3

Let's solve it with split and reducer.

def no_space(x)
    x.split(" ").reduce("") { |acc, cur| acc + cur}
end

Solution 4.1

Let's solve it with delete.

def no_space(x)
    x.delete(' ')
end

Solution 5.1

Let's solve it with gsub.

def no_space(x)
    x.gsub(" ","")
end
© 2021, Andrew Losseff