Help understanding Redacted


#1



Hello,

I have been relatively fine progressing through the Ruby course but am struggling with the basic understanding of the 'Redacted' section. It would be great if i could get a layman's explanation? just to better my understanding.

The bellow code is what i have pieced together, also from other help articles. Im pretty sure I'm missing a line somewhere but i don't know what :frowning:

*Im also confused as to why there is 2 different: Words & Word. Is word a pre-defined ruby term that it understands and not a set variable? because it hasn't been set by me.

Thank you!


https://www.codecademy.com/en/courses/ruby-beginner-en-mzrZ6/0/4?curriculum_id=5059f8619189a5000201fbcb

"Oops, try again. It looks like you didn't print each word from the user's text to the console."


puts "Information please:"
text = gets.chomp

puts "More information:"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
    if word == redact
    print "REDACTED"
else
    print word + " "
end
end


#2

@agner,

You are asked to give a text
text="this is a text"

Then you are asked to give a =word= which is assigned to the variable redact
for instance
redact = "text"

With
words= text.split(",")
you transform the text in a =list= and assign this =list= to the variable words
words = ["this","is","a","text"]

With
words.each do | word | .... end
you will iterate over the =list= words
and with each iteration an Element-Value is assigned to the variable word

With your print don't forget to add an extra space character....!!!


#3

@agner,
From
http://docs.ruby-lang.org/en/2.0.0/String.html#method-i-split

split(pattern=$;, [limit]) → anArray

Divides str into substrings based on a delimiter, returning an array of these substrings.

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ' were specified.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

When the input str is empty an empty Array is returned as the string is considered to have no fields to split.

" now's  the time".split        #=> ["now's", "the", "time"]
" now's  the time".split(' ')   #=> ["now's", "the", "time"]
" now's  the time".split(/ /)   #=> ["", "now's", "", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
"hello".split(//)               #=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3)            #=> ["h", "e", "llo"]
"hi mom".split(%r{\s*})         #=> ["h", "i", "m", "o", "m"]

"mellow yellow".split("ello")   #=> ["m", "w y", "w"]
"1,2,,3,4,,".split(',')         #=> ["1", "2", "", "3", "4"]
"1,2,,3,4,,".split(',', 4)      #=> ["1", "2", "", "3,4,,"]
"1,2,,3,4,,".split(',', -4)     #=> ["1", "2", "", "3", "4", "", ""]

"".split(',', -1)               #=> []

#4

Thanks for the detailed reply, though its a bit above my understanding, hehe.

It seems i was getting a bit ahead of myself, the particular lesson was failing because i had done an extra step that is for the next part :wink:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.