Is there a way to use gsub (or something else) in Ruby to replace a string with its hexadecimal equivalent? In Mysql you'd do something like this:
self.connection.execute("UPDATE `dvd_actor` SET actor = replace(actor, '£,', CHAR(163));")
I'm rewriting this in Rails and using gsub, something like this:
self.actor = actor.gsub(/£/, "£").strip if actor =~ /£/
But I already have all the lines written with the hexadecimal character and I'm trying to avoid finding out which character is which (some of them require copy/pasting because I don't have them in the english keyboard).
I tried this (which I saw in a post here):
actor.gsub(/"/) { "0x134".hex } if actor =~ /"/
But that doesn't do the trick, it produces a number.
Or better yet, maybe there's a gem that already does that? Basically take the HTML values and fix them? Oh, that would be nice.
I would try "0x134".hex.to_s(16). It converts "0x134" into "134".
I believe I found it: a gem called htmlentities is supposed to do just what I want. So I have this:
ampersands = where("actor LIKE ?", "%&%;%")
ampersands.each do |actor|
fixed_actor = fixer.decode(actor.actor)
self.update(actor.id, :actor => fixed_actor)
Related
Hy Folks
I got the Problem that i have to create an xml in ruby with builder, running on a sinatra server.
The Xml is filled with xml tags like this one:
<fu-ba:r test="test1" source="h1">
somthing
</fu-ba:r>
now i don't know how to get builder to create a tag like this one (the attributes are no Problem).
i Tried:
xml.fu-ba:r(......)
xml."fu-ba:r"(.......)
xml. << "fu-ba:r"(......)
Every idea or solution would help a lot, thanks Folks
Ruby identifiers are consist of alphabets, decimal digits, and the
underscore character, and begin with a alphabets(including
underscore). There are no restrictions on the lengths of Ruby
identifiers.
Since ruby identifiers don't allow the use of special characters builder has a method called tag! for this very purpose.
For example
x.tag!("fu-ba:r") {
x.text! "something"
}
Outputs
# <fu-ba:r>
# something
# </fu-ba:r>
I have a JSON string that looks like {\"heading\":\"Test\",\"id\":1} and I want to wipe the ID data from the string.
I've tried test.gsub(/\,\\"id\\"\:d+/, '') but that's not working.
How best to achieve this?
Sergio's JSON.parse is something you should consider. But baring that, those \'s you are seeing probably aren't really part of the string. That's just how irb is displaying it.
So test.gsub(/,"id":\d+/, '') should be what you want. (Also fixed a few other small bugs in the regex).
Let's say I have a string:
asd;;%$##!G'{}[]
Now I want to escape special symbols:
;&|><*?`$(){}[]!#
So, the output will be something like:
asd\;\;%\$#\#\!G\'\{\}\[\]
How can I achieve this using gsub/sub in Ruby?
test_value = "asd;;%$##!G'{}[]"
SPEC_REGEXP = /((;)|(\&)|(\|)|(>)|(<)|(\*)(\?)|(`)|(\$)|(\()|(\))|({)|(})|(\[)|(\])|(!)|(#))/
test_value.gsub!(SPEC_REGEXP,'\\\\\1')
Here's pretty much the same idea as in soundar's solution (but using character classes and no capturing):
"asd;;%$##!G'{}[]".gsub(/[;&|><*?`$(){}\[\]!#]/, '\\\\\\0')
In Ruby I have an arbitrary string, and I'd like to convert it to something that is a valid Unix/Linux filename. It doesn't matter what it looks like in its final form, as long as it is visually recognizable as the string it started as. Some possible examples:
"Here's my string!" => "Heres_my_string"
"* is an asterisk, you see" => "is_an_asterisk_you_see"
Is there anything built-in (maybe in the file libraries) that will accomplish this (or close to this)?
By your specifications, you could accomplish this with a regex replacement. This regex will match all characters other than basic letters and digits:
s/[^\w\s_-]+//g
This will remove any extra whitespace in between words, as shown in your examples:
s/(^|\b\s)\s+($|\s?\b)/\\1\\2/g
And lastly, replace the remaining spaces with underscores:
s/\s+/_/g
Here it is in Ruby:
def friendly_filename(filename)
filename.gsub(/[^\w\s_-]+/, '')
.gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
.gsub(/\s+/, '_')
end
First, I see that it was asked purely in ruby, and second that it's not the same purpose (*nix filename compatible), but if you are using Rails, there is a method called parameterize that should help.
In rails console:
"Here's my string!".parameterize => "here-s-my-string"
"* is an asterisk, you see".parameterize => "is-an-asterisk-you-see"
I think that parameterize, as being compliant with URL specifications, may work as well with filenames :)
You can see more about here:
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize
There's also a whole lot of another helpful methods.
How do I convert strings like "this is an example" to "this-is-an-example" under ruby?
The simplest version:
"this is an example".tr(" ", "-")
#=> "this-is-an-example"
You could also do something like this, which is slightly more robust and easier to extend by updating the regular expression:
"this is an example".gsub(/\s+/, "-")
#=> "this-is-an-example"
The above will replace all chunks of white space (any combination of multiple spaces, tabs, newlines) to a single dash.
See the String class reference for more details about the methods that can be used to manipulate strings in Ruby.
If you are trying to generate a string that can be used in a URL, you should also consider stripping other non-alphanumeric characters (especially the ones that have special meaning in URLs), or replacing them with an alphanumeric equivalent (example, as suggested by Rob Cameron in his answer).
If you are trying to make something that is a good URL slug, there are lots of ways to do it.
Generally, you want to remove everything that is not a letter or number, and then replace all whitespace characters with dashes.
So:
s = "this is an 'example'"
s = s.gsub(/\W+/, ' ').strip
s = s.gsub(/\s+/,'-')
At the end s will equal "this-is-an-example"
I used the source code from a ruby testing library called contest to get this particular way to do it.
If you're using Rails take a look at parameterize(), it does exactly what you're looking for:
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M001367
foo = "Hello, world!"
foo.parameterize => 'hello-world'