
-----------------------------------
Clayton
Wed Jan 03, 2007 6:44 pm

String.insert Problem
-----------------------------------
I'm currently writing a method for Rubidium that is supposed to take a sender's name, along with their message, and output it in rows 80 characters wide (ie, if you type in a message 100 characters long, you will get a line ~80 characters long 
def last_space(beginning_char, last_char, message)
    if last_char > message.length then
        last_char = message.length
    end
    beginning_char.upto(last_char) do |i|
        if message

-----------------------------------
Cervantes
Wed Jan 03, 2007 8:24 pm


-----------------------------------
A few problems.

First, conceptually, it'd be better to start at last_char and use Fixnum#downto to count down.
Second, the conditional for your if statement never succeeds. You've forgotten that a slice done like that returns an integer, which is the ASCII value of the character. To fix it, either change " " to a " "self, so 3.upto(5) {|x| puts x} would return 3.

-----------------------------------
Clayton
Wed Jan 03, 2007 9:03 pm


-----------------------------------
Thank you! It works just freakin' lovely now :D About the message
def last_space(beginning_char, last_char, message)
    if last_char > message.length then
        last_char = message.length
    end
    last_char.downto(beginning_char) do |i|
        if message

I e-mailed the whole thing to you Cervantes

-----------------------------------
wtd
Thu Jan 04, 2007 12:12 am


-----------------------------------
def last_space(beginning_char, last_char, message)
    last_char = message.length if last_char > message.length

    last_char.downto(beginning_char) do |i|
        return i if message[i, 1] == " "
      end
end 

:)

-----------------------------------
Clayton
Thu Jan 04, 2007 12:20 am


-----------------------------------
Does that make a difference in regards to speed? If it doesn't, then I prefer my way, as after you start jamming more and more code onto one line, things start getting unreadable.

-----------------------------------
rdrake
Thu Jan 04, 2007 12:37 am


-----------------------------------
Does that make a difference in regards to speed? If it doesn't, then I prefer my way, as after you start jamming more and more code onto one line, things start getting unreadable.I don't believe so, it just looks neater.  But if it makes code harder for you to understand, then I wouldn't bother shortening up the code that much.

-----------------------------------
Clayton
Thu Jan 04, 2007 12:53 am


-----------------------------------
Exactly, I could one line that method no problem, I just prefer not to. It just makes it that much harder to read when you try to cram all that code on to one line :?

For those of you who don't believe me :P


def last_space(beginning_char, last_char, message) last_char = message.length if last_char > message.length end last_char.downto(beginning_char) {|i| return i if message

-----------------------------------
Cervantes
Thu Jan 04, 2007 1:58 pm


-----------------------------------
You forgot semi-colons. That code won't execute.

-----------------------------------
ericfourfour
Thu Jan 04, 2007 5:16 pm


-----------------------------------
To me, both make equal sense so I prefer wtd's way because it is shorter.

last_char = message.length if last_char > message.lengthAssign the message length to the last character if the last character is greater than the message length.

if last_char > message.length then
  last_char = message.length
endIf the last character is greater than the message length, assign the message length to the last character.

Do you really need "then" in there?

-----------------------------------
wtd
Thu Jan 04, 2007 5:22 pm


-----------------------------------
Do you really need "then" in there?

No, it's not strictly necessary in this scenario.

Oh, and let me add another one:

last_char > message.length and last_char = message.length

-----------------------------------
Clayton
Thu Jan 04, 2007 7:28 pm


-----------------------------------

You forgot semi-colons. That code won't execute.


Shhhh! No one else noticed, so let's leave it at that :P

On-Topic: It's not even so much the way it makes sense, it's just that I don't like cramming different things on one line, I prefer to stick with one or two expressions/statements per line if I can help it, it just makes tracking the program easier for me.

-----------------------------------
wtd
Thu Jan 04, 2007 8:33 pm


-----------------------------------
It's just that, if you are only going to have the "if" branch, with no else, *and* only one thing happening if that condition is true (or if it's false, if "unless" is used), then by using the postfix "if" you avoid an unnecessary level of indentation and an unnecessary "end."  These added pieces of syntax greatly increase the visual complexity of the code.

-----------------------------------
ericfourfour
Fri Jan 05, 2007 1:05 am


-----------------------------------
Oh, and let me add another one:

last_char > message.length and last_char = message.length
I see how it works but why would it run? I thought you could only use "and" with booleans.

-----------------------------------
ericfourfour
Fri Jan 05, 2007 3:20 am


-----------------------------------
Well, I just figure out what it does and now I am actually finding it useful. Instead of having it return nil that makes it return false.

-----------------------------------
Cervantes
Fri Jan 05, 2007 1:53 pm


-----------------------------------
That's correct: you can only use "and" with booleans. However, what is a boolean? nil and false are defined to be false, and everything else is true.


Freakman, your last_space method is essentially String#rindex. Using that will make the code considerably nicer. :)
