2

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!

Daniel O'Connor
  • 2,012
  • 6
  • 24
  • 29
  • By 'it's not working' you mean when you display @list the title isn't updated? If so, that's because you aren't doing anything with your updated string. The 'new_title' is local and will be returned from the function, but you aren't grabbing it in the controller f_title = @list.fixlistname Will get the return from fixlistname, and it's up to you what to do with it. If you only want it for display and don't want to store the updated string you should just call fixlistname on the model from your view. – TheClair Apr 14 '10 at 18:07
  • Hi, sorry I wasn't more clear! I want it to modify the string so the fixed string can be saved to the database. The error message I'm getting is "undefined method `ends_with' for "You know you are a geek when":String" Thanks – Daniel O'Connor Apr 14 '10 at 18:22
  • You forgot the question mark on `.ends_with?` – jamuraa Apr 14 '10 at 18:51

4 Answers4

3

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
  • 5,592
  • 4
  • 46
  • 42
1

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.

Community
  • 1
  • 1
mmacaulay
  • 2,913
  • 21
  • 27
0

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 Running
  • 91,621
  • 15
  • 164
  • 165
-3

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
  • 9,396
  • 2
  • 32
  • 40
  • 1
    Regular expressions is definitely overkill for this situation, though. – Jordan Running Apr 14 '10 at 15:29
  • I've looked at regular expressions, but it's a bit over my head. Thanks for the suggestion though. – Daniel O'Connor Apr 14 '10 at 15:53
  • _A_ "way to inspect and modify strings ... is to use regular expressions". Regular expressions are hardly the only way, and, in the wrong hands, are the wrong way entirely. People seem to think they're the hammer in the toolbox, but they're not, they're a specialized tool, to be used with care lest they destroy what we're working on. – the Tin Man Dec 13 '12 at 15:05