Fatna
Fatna

Reputation: 93

ruby, how to remove the first part of string?

This is my first time posting in this forum, I am doing some selenium webdriver testing, I collected some data from an html UI, put it in an array:

consents = $driver.find_elements(:xpath,"//*[@id='main-display']/div[2]/div[2]/div/table/tbody/tr/td[7]//i")

, then I processed the array with map :

consent_values = consents.map { |consent| consent.attribute('class')}

printing the array I got :

["fa fa-check-circle", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle", "fa fa-circle-thin", "fa fa-circle-thin", "fa fa-check-circle,....]

1) Is there a way to do in the map to remove "fa" from all strings in the array ?

2) How about changing the strings "fa fa-check-circle" to be true and "fa fa-circle-thin" to be false so I will get : [true,false,true,....]

Thanks,

Upvotes: 1

Views: 175

Answers (2)

Fatna
Fatna

Reputation: 93

string[N..-1]

This worked for me. thanks anyway, I hope I will interact with this forum lot.

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Basically 2) eliminates the necessity of 1), so here you go:

consent_values = consents.map do |consent|
  case consent.attribute('class')
  when "fa fa-check-circle" then true
  when "fa fa-circle-thin" then false
  else nil
  end
end

To remove "fa " prefix from the string one might use String#[]:

"fa fa-check-circle"[/(?<=\Afa ).*/]
#⇒ "fa-check-circle"

Upvotes: 1

Related Questions