Forum: Ruby on Rails String Manipulation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-26 23:01
Hi all,

Help needed urgently


Example: this could be the string.

Kid games need to be both fun and educational. Aimed at ages pre-K
through middle school, safe environment to discover their abilities and
learn new skills with interactive and fun computer games. Our games
build skills in math, logic, memory, vocabulary, alphabet, spelling,
geography, computer skills, color identification, shape identification
and other various problem solving. Our commitment to parents, teachers,
and kids, is to connect learning and skill building with a sense of
challenge, fun, and self esteem.




Output
Kid games need to be both fun and educational. Aimed at ages pre-K
through =>
<= middle school, safe environment to discover their abilities and learn
new =>
<= skills with interactive and fun computer games. Our games build
skills in =>
.........
.........
=> a sense of challenge, fun, and self esteem.


Regards and thanks
B0f6756b62559d43213636dde11bb785?d=identicon&s=25 BenH (Guest)
on 2009-05-26 23:17
(Received via mailing list)
Is the point of your post to display the text at fixed line widths?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-26 23:18
(Received via mailing list)
On May 26, 10:01 pm, Ruby One <rails-mailing-l...@andreas-s.net>
wrote:
> Hi all,
>
> Help needed urgently

Maybe it's just me, but I think that in your hurry you've neglected to
describe what it is you want to do. Replace carriage returns with =>
\n<= ?
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-26 23:19
BenH wrote:
> Is the point of your post to display the text at fixed line widths?

yes like after 10 words i want to put => and than in next line starts
with <= and ends with => after 10 words and so on
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-26 23:21
Frederick Cheung wrote:
> On May 26, 10:01�pm, Ruby One <rails-mailing-l...@andreas-s.net>
> wrote:
>> Hi all,
>>
>> Help needed urgently
>
> Maybe it's just me, but I think that in your hurry you've neglected to
> describe what it is you want to do. Replace carriage returns with =>
> \n<= ?

its like after certain words i want to put => and than in next line
starts
with <= and than certain number of words and than ends with =>  and so
on
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-26 23:28
Ruby One wrote:
> Frederick Cheung wrote:
>> On May 26, 10:01�pm, Ruby One <rails-mailing-l...@andreas-s.net>
>> wrote:
>>> Hi all,
>>>
>>> Help needed urgently
>>
>> Maybe it's just me, but I think that in your hurry you've neglected to
>> describe what it is you want to do. Replace carriage returns with =>
>> \n<= ?
>
> its like after certain words i want to put => and than in next line
> starts
> with <= and than certain number of words and than ends with =>  and so
> on

please help!!!!
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-27 00:28
Ruby One wrote:
[...]
> please help!!!!

Just break the string up and insert the => stuff.  What exactly is the
problem?

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-27 00:30
Marnen Laibow-Koser wrote:
> Ruby One wrote:
> [...]
>> please help!!!!
>
> Just break the string up and insert the => stuff.  What exactly is the
> problem?
>
> Best,
> --
> Marnen Laibow-Koser
> http://www.marnen.org
> marnen@marnen.org

I am newbee so do not much syntax and loops can you please help me i
that.

thanks
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-27 00:45
Ruby One wrote:
[...]
>
> I am newbee so do not much syntax and loops can you please help me i
> that.
>
> thanks

Check out the "pickaxe book" (Programming Ruby, available on the Web).
Pay particular attention to regular expressions, the String class
(especially the split and join methods), the Array class, and looping
constructs such as each.  Then try writing your function.  If something
goes wrong that you can't figure out, tell us what you tried, and what
errors or other output you got.

Good luck!

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
B0f6756b62559d43213636dde11bb785?d=identicon&s=25 BenH (Guest)
on 2009-05-27 01:16
(Received via mailing list)
Try this:
words = "Kid games need to be both fun and educational. Aimed at ages
pre-K through middle school, safe environment to discover their
abilities and learn new skills with interactive and fun computer
games. Our games build skills in math, logic, memory, vocabulary,
alphabet, spelling, geography, computer skills, color identification,
shape identification and other various problem solving. Our commitment
to parents, teachers, and kids, is to connect earning and skill
building with a sense of challenge, fun, and self esteem."

line_length = 10

word_arry = words.split(" ")

text = (0..(words.length / line_length)).inject([]) {|v,num| start =
num * line_length; v << %Q{<= #{words[(start)...(start +
line_length)].join(" ")} =>}}

text.join("\n")


Shame on me...



On May 26, 3:45 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
F291b17f81647310d8a4284a26c879b2?d=identicon&s=25 Ruby One (rubyone)
on 2009-05-27 01:21
BenH wrote:
> Try this:
> words = "Kid games need to be both fun and educational. Aimed at ages
> pre-K through middle school, safe environment to discover their
> abilities and learn new skills with interactive and fun computer
> games. Our games build skills in math, logic, memory, vocabulary,
> alphabet, spelling, geography, computer skills, color identification,
> shape identification and other various problem solving. Our commitment
> to parents, teachers, and kids, is to connect earning and skill
> building with a sense of challenge, fun, and self esteem."
>
> line_length = 10
>
> word_arry = words.split(" ")
>
> text = (0..(words.length / line_length)).inject([]) {|v,num| start =
> num * line_length; v << %Q{<= #{words[(start)...(start +
> line_length)].join(" ")} =>}}
>
> text.join("\n")
>
>
> Shame on me...
>
>
>
> On May 26, 3:45�pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-




Hi,

Thanks for taking your time out for my problem.

i tried your code but it says

undefined method `join' for "Kid games ":String

what should i do now
Thanks and Regards
64aa4b69fdd7127e6f3ee16ae065a620?d=identicon&s=25 Giampiero Zanchi (giampiz)
on 2009-05-27 16:20
I am quite a beginner; it is not clean clode but works

number_of_words = 10
sentence = sentence.split(/\n/m).join(" ")
sentence.gsub!("  "," ")
r = /.*?[.!?](?:\s|$)/
sentences = sentence.scan(r)
sentences.map! {|e| e.split}
v = []
sentences.each do |e|
  while !e.empty?
    vv = []
    number_of_words.times {vv << (e.shift.to_s + " ")}
    v << vv
  end
end

v.each {|e| e.delete_if {|x| x == " "}}

v.map! {|e| e.join}

v.each {|e| p e}
This topic is locked and can not be replied to.