I'm working for almost 4 weeks with Ruby on Rails now, so consider me as a beginner.
When performing a conditional find query, I need to compare a part of an integer (put in a string..) with another integer.
#invs = Inv.find(:all, :conditions => {:cnr => INTEGER_TO_BE_COMPARED_TO })
Well, I found a way to get a certain part of an integer and wrote the following method:
def get_part_of_int
#r = Integer((#cnr % 10000000)/100000.0)
#r = "%02d" % #r
#r.to_s
end
This should give the output #r = 01 from #cnr = 40138355
The target is to only show invs with e.g. 01 as second and third digit.
I'm using:
Rails 3.2.8
Ruby 1.9.3p194
Thank you all in advance.
== Edit: ==
I realised there is no question mark in my post.. And there is some confusion.
The value of the column name cnr should be stripped to the 2nd and 3rd digit and be compared to a string e.g. 01.
Thank you #Salil for you suggestion.
However this doesn't work.. But I understand the way of using it. The following will convert the string :cnr to :nr.
Inv.find(:all, :conditions => {:cnr.to_s[1,2] => '01' })
Is there a way to compare a part of the value of the column with e.g. 01?
Maybe I should think the other way around. Is it possible to search for cnrs with the 2nd and 3rd digit e.g. 01? The trick is that these numbers are always as 2nd and 3rd digit. I assume a fuzzy search with e.g. Solr doesn't work, because when searching for 01 I don't want the cnr 40594013.
Can this be done in some way?
Thank you in advance and my apologies for confusing you.
if your question is how to get the second and third digit from a integer?
You can use following
40138355.to_s[1,2] ##This will give you "01"
EDITED Ref:- Substring
#invs = Inv.find(:all, :conditions => ["SUBSTRING(cnr,1,2) =?", #cnr.to_s[1,2] ])
Related
I'm using Rails and Nokogiri and I'm trying to parse some website.
This is where I'm stuck:
doc.css('#example > li:nth-child(1)').each do |node|
money = node.xpath('//*ul/li/div/span').text
end
It returns something like:
$100,000£230,000$40,000$9,000€600$800,000
I want to split those items, save them to the database and finally hand them to the view.
So, in the view, I want it to appear like:
(1)$100,000
(2)£230,000
(3)$40,000
(4)$9,000
(5)€600
(6)$800,000
I tried to split those items by this code below.
money = node.xpath('//*ul/li/div/span').text.split(/[$€£]/)
but the result looks like this:
["", "100,000", "230,000", "40,000", "9,000", "600", "800,000"]
And I don't know which item is in Dollar, Euro, or Pond.
Is there any good way to solve this problem?
you're almost there,
just use the positive lookahead :)
irb(main):005:0> "$100,000£230,000$40,000$9,000€600$800,000".split(/(?=[$£€])/)
=> ["$100,000", "£230,000", "$40,000", "$9,000", "€600", "$800,000"]
It needs a regular expression. This works:
"$100,000£230,000$40,000$9,000$600$800,000".scan(/([^\d][0-9,]+)/)
=> [["$100,000"],
["£230,000"],
["$40,000"],
["$9,000"],
["$600"],
["$800,000"]]
The regex contains these parts:
[^\d]: A character class matching a single non-digit. This will match the currency symbol.
`[0-9,]+': Another character class, this time repeating (the '+'). It matches the numeric part (0-9) plus the thousand's separator.
Is there currently a gem that's capable of taking strings, all in USD for this purpose, and converting them to a number? Some examples would be:
"$7,600" would turn into 7600
"5500" would turn into 5500
I know on the "5500" example I can just do "5500".to_i, but the spreadsheets being imported aren't consistent and some include commas and dollar signs while others do not. There a decent way of handling this across the board in Ruby?
I've tried something like money_string.scan(/\d/).join which seems to be fine, just worried I'll run into edge cases I haven't found yet, such as decimal places.
Why not remove all non-digit characters before calling .to_i
Example:
"$7,600".gsub(/\D/,'').to_i
And for a floating point number:
"$7,600.90".gsub(/[^\d\.]/, '').to_f
You can do:
"$100.00".scan(/[.0-9]/).join().to_f
or to_i if they're only dollars
You could use the Money gem
Money.parse("$100") == Money.new(10000, "USD")
You should be able to trim any non-numeric characters with a Ruby RegEx object. Sanitize your inputs to remove anything but numbers to the left of a decimal point, and then parse the numbers-only string as a number.
(And note that, if you're getting actual spreadsheets instead of CSV bunk, there's likely a value property you can read that ignores the text displayed on screen.)
def dollar_to_number(dollarPrice)
if dollarPrice
dollarPrice[1, dollarPrice.length].to_i
end
end
You can use the Monetize gem:
pry(main)> Monetize.parse("$7,600").to_i
=> 7600
https://github.com/RubyMoney/monetize
pry(main)> Monetize.parse("$7,600").class
=> Money
If you are using money-rails gem, you can do the following
irb(main):004:0> "$7,600".to_money("USD")
=> #<Money fractional:760000 currency:USD>
irb(main):005:0> "5500".to_money("USD")
=> #<Money fractional:550000 currency:USD>
Here is the code:
def autocomplete
if(params[:terms])
key = params[:terms]
customers = Customer.where(:$or => [
{:first_name => Regexp.new(/^#{key}/i)},
{:last_name => Regexp.new(/^#{key}/i)},
{:email => Regexp.new(/^#{key}/i)},
#{:phone => Regexp.new(/^#{key}[d+]/i)},
{:phone => Regexp.new(/^#{key.gsub(/\D+/,'')}/)},
{:zip_code => key.to_i },
{:street1 => Regexp.new(/#{key}/i)},
{:street2 => Regexp.new(/#{key}/i)}
]
)
The gsub method suggested by Tin Man gets me almost there - it strips any non-Digit characters from the search string only when searching in the :phone field in my DB.
The last problem is that the :phone field in the DB might actually have non-Digits in it (and I want to allow users to enter phone numbers however they want), so I need to temporarily ignore dashes when I'm searching (using find() in Mongo)
Not sure if I should do it at this level in the autocomplete function or if I should do it in the autocomplete.js module...
SUMMARY - I want to :phone.gsub(/\D+/,'') but gsub only works on strings, not a reference like this.
Some things I see:
Regexp.new(/^#{key}[d+]/i)}
[\d+] is nonsense. Drop the surrounding [].
For:
{:zip_code => key.to_i },
Don't convert the zipcode to an integer. Some zip codes are hyphenated, which will drop the trailing value. Also, unless you intend to perform math on the value, leave it as a string.
What is $or? Using a global is usually a sign of code-smell. There are few reasons to use one in Ruby, and I've never found a good use for one in my code, and it's something that can usually be refactored out easily using a constant.
I think you actually answered my question by pointing out the key.to_i for ZIP - that's actually exactly what I WANT to do with Phone Number - strip out all the dashes, spaces, brackets, etc. I am going to give that a try.
No, no, no, no. to_i won't do what you want. '0-1'.to_i => 0 and '0.1'.to_i => 0. Instead you want to strip out all non-numeric characters from the string using gsub, then you're done:
'0.1'.gsub(/\D+/, '')
=> "01"
'123-456-7890'.gsub(/\D+/, '')
=> "1234567890"
'(123) 456 7890'.gsub(/\D+/, '')
=> "1234567890"
'0.1'.gsub(/\D+/, '').to_i
=> 1
Note what happened above when to_i received the "01", the leading zero was removed because it wasn't significant for the representation of an Fixnum. You can force it to display using a string format, but why? A phone number is NOT a numeric value, it's a string value, even though it is a bunch of numbers. We NEVER need to do addition on them, or any math, so it's senseless to convert it to an integer. Keep it as a string of digits.
How would I detect if I have this scenario, I would be getting this inputs
3b => allow
4b => allow
55b => allow
1111bbbb => allow
num45 => no !
and if I do allow given, I wold also like to remove all characters that are not numbers
3b => 3
555B => 555
11 => 11
I have tried to check if the given input is numeric or not, but this condition is out of scope of my knowledge.
Thanks for your time and consideration.
You can use:
/\A(\d+)[a-z]*\z/i
If the expression matches your desired number will be in the first capturing group.
Example at Rubular. (It uses ^/$ instead of \A/\z just for the demonstration, you should use \A/\z.)
This will look for integer + string and convert it to an integer. It will ignore a string + integer input.
input = '45num'
if input.match(/\d+[a-zA-Z]+/)
result = input.to_i
end
result => 45
You really want to use: str[/\A\d+/] - This will give you the leading digits or nil.
Hmm I am no regex ninja but I think you could use: ^([\d]+) to capture JUST the number. give it a try here
if the string begins with a number
!/^[0-9]/.match(#variable).nil?
if it does, get only the number part
#variable = #variable.gsub(/[^0-9]/, '')
I have an ActiveRecord model, Foo, which has a name field. I'd like users to be able to search by name, but I'd like the search to ignore case and any accents. Thus, I'm also storing a canonical_name field against which to search:
class Foo
validates_presence_of :name
before_validate :set_canonical_name
private
def set_canonical_name
self.canonical_name ||= canonicalize(self.name) if self.name
end
def canonicalize(x)
x.downcase. # something here
end
end
I need to fill in the "something here" to replace the accented characters. Is there anything better than
x.downcase.gsub(/[àáâãäå]/,'a').gsub(/æ/,'ae').gsub(/ç/, 'c').gsub(/[èéêë]/,'e')....
And, for that matter, since I'm not on Ruby 1.9, I can't put those Unicode literals in my code. The actual regular expressions will look much uglier.
ActiveSupport::Inflector.transliterate (requires Rails 2.2.1+ and Ruby 1.9 or 1.8.7)
example:
>> ActiveSupport::Inflector.transliterate("àáâãäå").to_s
=> "aaaaaa"
Rails has already a builtin for normalizing, you just have to use this to normalize your string to form KD and then remove the other chars (i.e. accent marks) like this:
>> "àáâãäå".mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s
=> "aaaaaa"
Better yet is to use I18n:
1.9.3-p392 :001 > require "i18n"
=> false
1.9.3-p392 :002 > I18n.transliterate("Olá Mundo!")
=> "Ola Mundo!"
I have tried a lot of this approaches but they were not achieving one or several of these requirements:
Respect spaces
Respect 'ñ' character
Respect case (I know is not a requirement for the original question but is not difficult to move an string to lowcase)
Has been this:
# coding: utf-8
string.tr(
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
)
– http://blog.slashpoundbang.com/post/12938588984/remove-all-accents-and-diacritics-from-string-in-ruby
You have to modify a little bit the character list to respect 'ñ' character but is an easy job.
My answer: the String#parameterize method:
"Le cœur de la crémiére".parameterize
=> "le-coeur-de-la-cremiere"
For non-Rails programs:
Install activesupport: gem install activesupport then:
require 'active_support/inflector'
"a&]'s--3\014\xC2àáâã3D".parameterize
# => "a-s-3-3d"
Decompose the string and remove non-spacing marks from it.
irb -ractive_support/all
> "àáâãäå".mb_chars.normalize(:kd).gsub(/\p{Mn}/, '')
aaaaaa
You may also need this if used in a .rb file.
# coding: utf-8
the normalize(:kd) part here splits out diacriticals where possible (ex: the "n with tilda" single character is split into an n followed by a combining diacritical tilda character), and the gsub part then removes all the diacritical characters.
I think that you maybe don't really what to go down that path. If you are developing for a market that has these kind of letters your users probably will think you are a sort of ...pip.
Because 'å' isn't even close to 'a' in any meaning to a user.
Take a different road and read up about searching in a non-ascii way. This is just one of those cases someone invented unicode and collation.
A very late PS:
http://www.w3.org/International/wiki/Case_folding
http://www.w3.org/TR/charmod-norm/#sec-WhyNormalization
Besides that I have no ide way the link to collation go to a msdn page but I leave it there. It should have been http://www.unicode.org/reports/tr10/
This assumes you use Rails.
"anything".parameterize.underscore.humanize.downcase
Given your requirements, this is probably what I'd do... I think it's neat, simple and will stay up to date in future versions of Rails and Ruby.
Update: dgilperez pointed out that parameterize takes a separator argument, so "anything".parameterize(" ") (deprecated) or "anything".parameterize(separator: " ") is shorter and cleaner.
Convert the text to normalization form D, remove all codepoints with unicode category non spacing mark (Mn), and convert it back to normalization form C. This will strip all diacritics, and your problem is reduced to a case insensitive search.
See http://www.siao2.com/2005/02/19/376617.aspx and http://www.siao2.com/2007/05/14/2629747.aspx for details.
The key is to use two columns in your database: canonical_text and original_text. Use original_text for display and canonical_text for searches. That way, if a user searches for "Visual Cafe," she sees the "Visual Café" result. If she really wants a different item called "Visual Cafe," it can be saved separately.
To get the canonical_text characters in a Ruby 1.8 source file, do something like this:
register_replacement([0x008A].pack('U'), 'S')
You probably want Unicode decomposition ("NFD"). After decomposing the string, just filter out anything not in [A-Za-z]. æ will decompose to "ae", ã to "a~" (approximately - the diacritical will become a separate character) so the filtering leaves a reasonable approximation.
iconv:
http://groups.google.com/group/ruby-talk-google/browse_frm/thread/8064dcac15d688ce?
=============
a perl module which i can't understand:
http://www.ahinea.com/en/tech/accented-translate.html
============
brute force (there's a lot of htose critters!:
http://projects.jkraemer.net/acts_as_ferret/wiki#UTF-8support
http://snippets.dzone.com/posts/show/2384
I had problems getting the foo.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s solution to work. I'm not using Rails and there was some conflict with my activesupport/ruby versions that I couldn't get to the bottom of.
Using the ruby-unf gem seems to be a good substitute:
require 'unf'
foo.to_nfd.gsub(/[^\x00-\x7F]/n,'').downcase
As far as I can tell this does the same thing as .mb_chars.normalize(:kd). Is this correct? Thanks!
If you are using PostgreSQL => 9.4 as your DB adapter, maybe you could add in a migration it's "unaccent" extension that I think does what you want, like this:
def self.up
enable_extension "unaccent" # No falla si ya existe
end
In order to test, in the console:
2.3.1 :045 > ActiveRecord::Base.connection.execute("SELECT unaccent('unaccent', 'àáâãäåÁÄ')").first
=> {"unaccent"=>"aaaaaaAA"}
Notice there is case sensitive up to now.
Then, maybe use it in a scope, like:
scope :with_canonical_name, -> (name) {
where("unaccent(foos.name) iLIKE unaccent('#{name}')")
}
The iLIKE operator makes the search case insensitive. There is another approach, using citext data type. Here is a discussion about this two approaches. Notice also that use of PosgreSQL's lower() function is not recommended.
This will save you some DB space, since you will no longer require the cannonical_name field, and perhaps make your model simpler, at the cost of some extra processing in each query, in an amount depending of whether you are using iLIKE or citext, and your dataset.
If you are using MySQL maybe you can use this simple solution, but I have not tested it.
lol.. i just tryed this.. and it is working.. iam still not pretty sure why.. but when i use this 4 lines of code:
str = str.gsub(/[^a-zA-Z0-9 ]/,"")
str = str.gsub(/[ ]+/," ")
str = str.gsub(/ /,"-")
str = str.downcase
it automaticly removes any accent from filenames.. which i was trying to remove(accent from filenames and renaming them than) hope it helped :)