views:

213

answers:

4

Hey everyone,

So I'm new to Rails (teaching myself as a senior project in high school), and I'm trying to figure out how to modify these strings.

Let's say someone writes the following string in a form: "you know you are a geek when"

How can I automatically change it to this: "You know you are a geek when..."?

I need Rails to check the case of the first letter and check for the three dots then modify the string as necessary. I've looked here, but I can't find anything that would work.

Thanks a lot!

EDIT: I'm trying to implement what Reuben Mallaby suggested, but I'm having trouble. Here's the relevant part of the lists_controller.rb file, and below that is the method in the list.rb model.

def update
  @list = List.find(params[:id])
  @list.fixlistname
  respond_to do |format|
    if @list.update_attributes(params[:list])
      flash[:notice] = 'List was successfully updated.'
      format.html { redirect_to(@list) }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @list.errors, :status => :unprocessable_entity }
    end
  end
end

and..

def fixlistname
  new_title = title.humanize + (title.ends_with("...") ? "" : "...")
end

EDIT 2: I want the string to be modified before it goes into the database, and this is the error message I'm getting:

undefined method `ends_with' for "You know you are a track runner when":String

GOT IT WORKING! THANKS EVERYONE!

+3  A: 

You could use humanize to make sure that the first letter is uppercase and ends_with?("...") to check for the three dots

new_string = my_string.humanize + (my_string.ends_with?("...") ? "" : "...")
Reuben Mallaby
Hi, thanks for the help. I'm having trouble implementing it though. I've edited the question with my code.
Daniel O'Connor
if fixlistname is in your List model....def fixlistname self.title = self.title.humanize + (self.title.ends_with("...") ? "" : "...")end
Reuben Mallaby
I've tried that and I'm getting the same error message:undefined method `ends_with' for "You know you are a geek when":String
Daniel O'Connor
gah, have your tried ends_with?("...") with the ? that I forgot at first? X(
Reuben Mallaby
Thank you! It worked after I added the method as a validation :D
Daniel O'Connor
A: 

The way to inspect and modify strings with ruby is to use regular expressions. You can read about them here, and learn to use them at Rubular

mckeed
Regular expressions is definitely overkill for this situation, though.
Jordan
and for a beginner too
cbrulak
I've looked at regular expressions, but it's a bit over my head. Thanks for the suggestion though.
Daniel O'Connor
A: 

String#capitalize will capitalize the first letter of a string:

"you know you are a geek when".capitalize # => "You know you are a geek when"

And can get the last three characters of a string using String#[]:

"you know"[-3,3]             # => "now"
"you know..."[-3,3]          # => "..."

"you know..."[-3,3] == "..." # => true

Rails also provides a nice shortcut for this in the form of String#ends_with?:

"you know...".ends_with?("...") # => true

So you can check the end of the string, and if it isn't "...", you can append "...":

dots = "..."
my_str = "you know you are a geek when"

my_str = my_str + dots unless my_str.ends_with? dots
# => "you know you are a geek when..."

my_str.capitalize!
puts my_str
# => "You know you are a geek when..."

One edge case you may want to consider, though, is if the string ends with periods, but not three of them. For example, how will you handle a string like "you know.." (the above method would make this "You know.....") or "you know....." (the above would leave this unchanged)?

Jordan
the `.endsWith` extension is probably a bit more clear than `[-3,3]`.
ryeguy
+1  A: 

Just a tip, but when you perform "destructive" operations (operations that modify the calling object) you should add a exclamation mark (!) to the end of the method name. It's just a convention, but it definitely increases readability, and decreases surprise gotchas later on.

So in your controller you would have:

@list.fixlistname!

and in your model you would have:

def fixlistname!
  new_title = title.humanize + (title.ends_with?("...") ? "" : "...")
end

See here for more info.

mmacaulay