HelloWorld
HelloWorld

Reputation: 11239

Making a method that makes a string CamelCase

I am trying to work through this problem.

Add a method named add_index to the Array class. The method should take the index value of an element's position and add it to the String in the same position.

Hint: the each_with_index method should help you solve this problem. Try to find the each_with_index method yourself in Ruby Docs, and learn how it's used.

Here is my current code: **I am not creating the full class or using self, I am just testing how self would act with the variable a. In the test, self is pass "This is a test".

new-host:~$ irb
2.0.0p247 :001 > a = "This is a test"
 => "This is a test" 
2.0.0p247 :002 > a.split.join.downcase
 => "thisisatest" 
2.0.0p247 :003 > a.split
 => ["This", "is", "a", "test"] 
    #Should I do something with the array first? I could pass them into a code block to call the .capitalize method on them...with this in mind I am not sure how to skp the [0] of the array.
2.0.0p247 :004 > a.split.join.downcase[0..3]
 => "this" 

2.0.0p247 :005 > a.split.join.downcase.capitalize
 => "Thisisatest"

Ultimately I need the "This is a test" to look like "thisIsATest". I've been trying to figure this out for a while now. If anyone could give some insight I would appreciate it. Thanks!

One idea I have is to do something like this:

a.split.each do |num| num.to_s.downcase
2.0.0p247 :010?>   end
#I know this isn't right but from the syntax I know I think doing something like this is a step in the right direction.

"This is a test" is what's being run through the test:

describe "String" do
  describe "camel_case" do
    it "leaves first word lowercase" do
      "test".camel_case.should eq("test")
    end
    it "should lowercase first letter if it isn't" do
      "Test".camel_case.should eq("test")
    end
    it "should combine words using camel case" do
      "This is a test".camel_case.should eq("thisIsATest")
    end
  end
end

I am excluding the class String and def camel_case for my method. I am just trying to test the block of code for my method.

Upvotes: 1

Views: 1478

Answers (8)

rderoldan1
rderoldan1

Reputation: 4078

This is how i solved camelcase problem

a = "This is a test"
a.split.map(&:capitalize}.join
"ThisIsATest"

Update

a.split.each_with_index.map { |i, el| (i.capitalize unless el.eql? 0) || i.downcase}.join

or

str = "This is a test".split
str[0].downcase + str[1..-1].map(&:capitalize).join

Upvotes: 1

mirelon
mirelon

Reputation: 4996

The most simple way:

"This is a test".tr(' ','_').camelize(:lower)
# "thisIsATest"

Upvotes: 0

Borodin
Borodin

Reputation: 126722

def camel_case(str)
  str.downcase.split.each_with_index.map { |v,i|  i == 0 ? v : v.capitalize }.join
end

OR

def camel_case(str)
  words = str.downcase.split
  words.shift + words.map(&:capitalize).join
end

puts camel_case('This is a test')

output

thisIsATest

Upvotes: 3

Shoe
Shoe

Reputation: 76240

Here's another way of doing it:

def camel_case(str)
    ary = str.split
    ary[0].downcase + ary[1..-1].map(&:capitalize).join
end

example usage:

camel_case "This is a test"
# => "thisIsATest"

Reference:

Upvotes: 0

epsilon
epsilon

Reputation: 2969

a.split.map(&:capitalize).join

@edit: With gsub

a.gsub(/ \w+/) { |w| w.strip.capitalize }.gsub(/^\w/) {|w| w.downcase }

Upvotes: 0

ptierno
ptierno

Reputation: 10074

This should do the trick.

a = "This is a test".split.map!(&:capitalize).join.sub!(/^\w{1}/) { |first| first.downcase }

Upvotes: 0

Stefan
Stefan

Reputation: 114138

You'll need each_with_index to distinguish between the first word and the rest. Something like:

"This is a test".split.each_with_index.map { |word, index|
  if index == 0
     # transform first word
  else
     # transform other words
  end
}.join

The first transformation could be word.downcase, the other word.capitalize.

Upvotes: 0

tigeravatar
tigeravatar

Reputation: 26640

Here's one way...

s_original = "This is a test"
s_converted = ""
s_original.downcase.split(" ").each_with_index {|word, i| s_converted = "#{s_converted}#{if i > 0 then word.capitalize! else word end}"}

puts s_original #=> "This is a test"
puts s_converted #=> "thisIsATest"

Upvotes: 0

Related Questions