Get index of string scan results in ruby
Try this:
res = []
"abab".scan(/a/) do |c|
res << [c, $~.offset(0)[0]]
end
res.inspect # => [["a", 0], ["a", 2]]
Get the index of all characters in ruby
Use Enumerator#with_index
:
str = "sssaadd"
str.each_char.with_index do |char, index|
puts "#{index}: #{char}"
end
How do I find the index of a character in a string in Ruby?
index(substring [, offset]) → fixnum or nil
index(regexp [, offset]) → fixnum or nil
Returns the index of the first occurrence of the given substring or pattern (regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to begin the search.
"hello".index('e') #=> 1
"hello".index('lo') #=> 3
"hello".index('a') #=> nil
"hello".index(?e) #=> 1
"hello".index(/[aeiou]/, -3) #=> 4
Check out ruby documents for more information.
Return index of all occurrences of a character in a string in ruby
s = "a#asg#sdfg#d##"
a = (0 ... s.length).find_all { |i| s[i,1] == '#' }
View results of a search in index
With this code, i finally achieved what I was trying to do.
<div class="container">
<div class="row">
<% if params[:term].present? %>
<% if @products_search.present? %>
<% @products_search.each do |product| %>
<div class="col-sm-8 col-sm-offset-2">
<%= product.name %><br>
<%= product.description %><br>
<%= humanized_money_with_symbol(product.price)%><br>
<%= link_to "Show Product", product_path(product) %>
</div>
<%end%>
<% else %>
<h1>No products found!</h1>
<%end%>
<% else %>
<% @products.each do |product| %>
<div class="col-sm-8 col-sm-offset-2">
<%= product.name %><br>
<%= product.description %><br>
<%= humanized_money_with_symbol(product.price)%><br>
<%= link_to "Show Product", product_path(product) %>
</div>
<%end%>
<%end%>
</div>
</div>
How to get index with multiple values but return single result
after further researching, the code below works :
diff = []
actual[row_num].join(",").scan(actual[row_num][col_num]) do |c|
diff << [c, Regexp.last_match.offset(0).first]
end
I applied this on the logic of my code. I first converted the Array to String using JOIN including Commas. And scan each ROW & COLUMN.
I then call each result as diff.last[1], to get the exact INDEX.
Hopefully this helps someone the same as my problem.
Find indices of elements that match a given condition
Ruby 1.9:
arr = ['x', 'o', 'x', '.', '.', 'o', 'x']
p arr.each_index.select{|i| arr[i] == 'x'} # =>[0, 2, 6]
Code
ruby regex: match and get position(s) of
Using Ruby 1.8.6+, you can do this:
require 'enumerator' #Only for 1.8.6, newer versions should not need this.
s = "AustinTexasDallasTexas"
positions = s.enum_for(:scan, /Texas/).map { Regexp.last_match.begin(0) }
This will create an array with:
=> [6, 17]
In Ruby, how do I find the index of an element in an array in a case-insensitive way?
Use Array#find_index
:
a = ["A", "B", "C"]
a.find_index {|item| item.casecmp("b") == 0 }
# or
a.find_index {|item| item.downcase == "b" }
Note that the usual Ruby caveats apply for case conversion and comparison of accented and other non-Latin characters. This will change in Ruby 2.4. See this SO question: Ruby 1.9: how can I properly upcase & downcase multibyte strings?
In Ruby how do I find the index of one of an array of elements?
find_index
takes a single element. You could find the minimum by doing something like
a = ["a", "b", "c"]
to_find = ["b", "c"]
to_find.map {|i| a.find_index(i) } .compact.min # => 1
Related Topics
Best Way to Debug Third-Party Gems in Ruby
Ruby Sleep or Delay Less Than a Second
How to Detect Certain Unicode Characters in a String in Ruby
Sum the Value of Array in Hash
How to Cleanly Initialize Attributes in Ruby with New
Running into Smtp Error When Trying to Send Email in Ror App
In 'Require': No Such File to Load -- Iconv (Loaderror)
Capturing Groups Don't Work as Expected with Ruby Scan Method
Difference Between Has_One and Belongs_To in Rails
In Ruby's Test::Unit::Testcase, How to Override the Initialize Method
How to Configure Capistrano to Use My Rvm Version of Ruby
How to Check If a Value Is a Number
Ruby on Rails - Drop Down Box on Change Event
Asynchronous Http Request in Ruby
Why Many People Use "-%>" Instead of "%>" in Rails
Always Getting 401 Unauthorized with New Install of Rails + Devise