I’ve started with Ruby a month ago and I am doing some works with
strings
and regular expressions. I am trying to take a long text and store the
individual sentences in an array. I can split a sentence in words and
store them in an array, but I cannot manage to do it with sentences.
I have used the following assignment to work with the words:
str = “Ruby is great”
words = []
words = str.scan(/\w+/)
The result is words[0]=“Ruby” words[1]=“is” and words[3]=“great”
I would like to do the following:
str = “Ruby is great. We all know that.”
and get words[0]=“Ruby is great” and ruby[1]=“We all know that”
Any ideas on how to do it with a regular expression instead of looping
through the string looking for the “.”?
I did not understand if you want to split the string on the full stop
str.split(".")
or divide the string in words and split them in two groups:
str = “Ruby is great. We all know that.”
([(v=str.split(" “))[0…k=((l=(v.size))/2)]]+[v[k…l]]).map{|e|e.join(”
")}
=> [“Ruby is great.”, “We all know that.”]
[Note: parts of this message were removed to make it a legal post.]
Hello guys,
[cut]
I would like to do the following:
str = “Ruby is great. We all know that.”
and get words[0]=“Ruby is great” and ruby[1]=“We all know that”
Any ideas on how to do it with a regular expression instead of looping
through the string looking for the “.”?
Hi,
maybe you should to try this: words = str.split(/.\s*/)
it works for me:
irb(main):008:0> str = “Ruby is great. We all know that.”
=> “Ruby is great. We all know that.”
irb(main):009:0> words = str.split(/.\s*/)
=> [“Ruby is great”, “We all know that”]
irb(main):010:0> words[0]
=> “Ruby is great”
irb(main):011:0> words[1]
=> “We all know that”
and get words[0]=“Ruby is great” and ruby[1]=“We all know that”
=> “Ruby is great. We all know that.”
irb(main):009:0> words = str.split(/.\s*/)
=> [“Ruby is great”, “We all know that”]
irb(main):010:0> words[0]
=> “Ruby is great”
irb(main):011:0> words[1]
=> “We all know that”
greetings
even more simple
irb(main):001:0> “Ruby is great. We all know that.”.split(".")
=> [“Ruby is great”, " We all know that"]
I’ve started with Ruby a month ago and I am doing some works with
strings
and regular expressions. I am trying to take a long text and store the
individual sentences in an array. I can split a sentence in words and
store them in an array, but I cannot manage to do it with sentences.
I have used the following assignment to work with the words:
str = “Ruby is great”
words = []
words = str.scan(/\w+/)
The result is words[0]=“Ruby” words[1]=“is” and words[3]=“great”
I would like to do the following:
str = “Ruby is great. We all know that.”
and get words[0]=“Ruby is great” and ruby[1]=“We all know that”
Any ideas on how to do it with a regular expression instead of looping
through the string looking for the “.”?
On Sat, Jun 21, 2008 at 2:23 AM, Bryan JJ Buckley [email protected]
wrote:
You can split on a regex for a full-stop followed by (optional) whitespace.
str.split(/.\s?/)
=> [“Ruby is great”, “We all know that”]
str=“Dr. Feelgood will meet you at the corner of Foo St. and Bar Dr.
tonight at 8:00; bring $2.98 – exact change – to resolve the 5.5%
interest you owe.”
Btw, I rather tend to make it a requirement that the argument has the
appropriate type. Since #gsub is capable of working with String and
Regexp as pattern, I would not change your method’s implementation but
the code invoking it.
Taking this one step further: I would choose a different abstraction:
def transform from_file, to_file
repl = yield(File.read(from_file)) and
File.open(to_file, “w”) do |io|
io.write(repl)
end
end
Then you can do
transform “data”, “result” do |content|
content.gsub! /[aeiou]/, “*”
content
end