Comparision of a regexp in ruby - ruby

I want to make a case sensitive comparision like this:
If ARGV[0].eql? /word/i
print "yep! ^^"
elsif
print "nope :("
end
But that don't works... Don't recognizes "word"
I've tryed too quoteing and escaping chars...
What i'm doing wrong?
Thankyou

Probably you should use =~ operator, then
"word" =~ /word/i
works fine
http://www.regular-expressions.info/ruby.html

The problem is that you are trying to compare a regex to a string. By changing /word/i to 'word' it should work.

Related

What is wrong with this extremely simple regex?

I'm trying to test that a regex will match a 2-digit number. I get:
11 =~ /^\d{1,2}$/
# => nil
Yet the regex works flawlessly on Rubular. What am I doing wrong?
The problem is that you are testing the regex against a number and not a string. Regexes are intended for matching strings. Simply:
'11' =~ /^\d{1,2}$/
or
11.to_s =~ /^\d{1,2}$/
You are calling Kernel#=~, which always returns nil.
Rubular does not interpret your input as Ruby code, it interprets is as string literal. That is why it works there.
You are applying regex on number instead of string so convert it to string and try again.

How would I test if a string contains a certain word (using Ruby)?

I want to test to see if a string called note.attachment_content_type contains the word "image". How would I do this? Thanks
Use String#include?
note.attachment_content_type.include?("image")
You can use Regexp:
if note.attachment_content_type =~ /image/
puts "Found"
end
I would use String#[]
note.attachment_content_type[/image/]

Why is the IRB prompt changing to an askerisk when I try to match this regex?

I am a newbie in Ruby, I'm using version 1.9.3. I have the following regular expression:
/\\\//
As far as I know, it should match a string which has the characters '\' and '/', one following the other, right?
I am using the following code in order to get true in case the regex matches the string or symbol in the far right:
!(regex !~ :"string or symbol to match")
Because using =~ gives me the index of the match and I simply want a boolean. Besides, I'm trying to see how ugly or hackish can Ruby look compared to C :P
When I try to match the symbol :\/ the IRB prompt changes to an asterisk, and returns nothing. Why?
When I try to match the string "\/" my little ugly snippet returns false. Why?
The symbol :\/ is not a valid symbol. You could do :'\/' if you wanted a symbol version of the string '\/'. And when you feed it "\/" it is false because that has double quotes so it is actually the string '/' so you actually want either '\/' or "\\/".
Finally, it's better code and convention to do your test like so:
!!(regex =~ :'\/')
!!(regex =~ '\/')
!!(regex =~ "\\/")

Stripping non-alphanumeric chars but leaving spaces in Ruby

Trying to change this:
"The basketball-player is great! (Kobe Bryant)"
into this:
"the basketball player is great kobe bryant"
Want to downcase and remove all punctuation but leave spaces...
Tried string.downcase.gsub(/[^a-z ]/, '') but it removes the spaces
You can simply add \s (whitespace)
string.downcase.gsub(/[^a-z0-9\s]/i, '')
If you want to catch non-latin characters, too:
str = "The basketball-player is great! (Kobe Bryant) (ひらがな)"
str.downcase.gsub(/[^[:word:]\s]/, '')
#=> "the basketballplayer is great kobe bryant ひらがな"
Some fine solutions, but simplest is usually best:
string.downcase.gsub /\W+/, ' '
All the other answers strip out numbers as well. That works for the example given but doesn't really answer the question which is how to strip out non-alphanumeric.
string.downcase.gsub(/[^\w\s]/, '')
Note this will not strip out underscores. If you need that then:
string.downcase.gsub(/[^a-zA-Z\s\d]/, '')
a.downcase.gsub(/[^a-z ]/, "")
Note the whitespace I have added after a-z.
Also if you want to replace all whitespaces(not only space use \s as proposed by gmalette).
All the previous answers make basketball-player into basketballplayer or remove numbers entirely, which is not exactly what is required.
The following code does exactly what you asked:
text.downcase
.gsub(/[^[:word:]\s]/, ' ') # Replace sequences of non-alphanumerical chars by a single space
Hope this helps someone!

How to remove newLines at the beginning and at the end of a Ruby string

I need to remove newlines at the beginning and at the end of a string in ruby (some sort of trimming).
But JUST at the beginning and the end... The new lines located in the middle of the string must remain untouched.
Thank you!
You can use String#strip method.
"\tgoodbye\r\n".strip #=> "goodbye"
String.strip will remove all extra whitespace from the front and back, leaving innards alone.
http://ruby-doc.org/core/classes/String.html#M001189
This should do it:
string.lstrip!.rstrip!
If your intent is to strip just whitespace then the strip method should work...but if your trying to target new lines specifically then maybe try this:
"\r\na b c d\r\ne f g\r\n".gsub(/^\r\n/, "").gsub(/\r\n$/, "")
=> "a b c d\r\ne f g"
the gsub method will use regular expression to target the beginning ^ and end $ locations for replacement with "".
NOTE: Here I made the assumption that your newline is \r\n. This may not be platform independent.
The platform independent version of NPatel's answer is:
"\nabc\ndef\n".gsub(/^#{$/}/, "").gsub(/#{$/}$/, "")

Resources