Is there a workaround to open URLs containing underscores in Ruby?
This looks like a bug in URI, and uri-open, HTTParty and many other gems make use of URI.parse.
Here's a workaround:
require 'net/http'
require 'open-uri'
def hopen(url)
begin
open(url)
rescue URI::InvalidURIError
host = url.match(".+\:\/\/([^\/]+)")[1]
path = url.partition(host)[2] || "/"
Net::HTTP.get host, path
end
end
resp = hopen("http://dear_raed.blogspot.com/2009_01_01_archive.html")
Routes with Dash `-` Instead of Underscore `_` in Ruby on Rails
With Rails 3 and later you can do like this:
resources :user_bundles, :path => '/user-bundles'
Another option is to modify Rails, via an initializer.
I don't recommend this though, since it may break in future versions (edit: doesn't work in Rails 5).
Using :path
as shown above is better.
# Using private APIs is not recommended and may break in future Rails versions.
# https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L1012
#
# config/initializers/adjust-route-paths.rb
module ActionDispatch
module Routing
class Mapper
module Resources
class Resource
def path
@path.dasherize
end
end
end
end
end
end
RoR: How to retain underscore from URL parameters
The accepted solution (in comments) was using -
instead of _
and replace -
to _
in the controller.
Ruby : cannot open a link that works in a web browser
Using OpenURI is fine in a pinch, but you'd be better served by a more robust networking library like Net::HTTP or Typhoeus:
response = Typhoeus.get('http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200')
response.body #=> binary image data
(Note: tested this before sharing — it loads fine)
Strange underscore param in remote links
it's a cache buster. It's also used in development mode, so to avoid getting an old request from the browser cache.
(unfortunately, all the explanations I found are realated to advertisement :S)
Ruby Convert string into undescore, avoid the / in the resulting string
Solutions:
"CommonCar::RedTrunk".gsub(':', '').underscore
or:
"CommonCar::RedTrunk".sub('::', '').underscore
or:
"CommonCar::RedTrunk".tr(':', '').underscore
Alternate:
Or turn any of these around and do the underscore()
first, followed by whatever method you want to use to replace "/" with "_".
Explanation:
While all of these methods look basically the same, there are subtle differences that can be very impactful.
In short:
gsub()
– uses a regex to do pattern matching, therefore, it's finding any occurrence of":"
and replacing it with""
.sub()
– uses a regex to do pattern matching, similarly togsub()
, with the exception that it's only finding the first occurrence (the "g" ingsub()
meaning "global"). This is why when using that method, it was necessary to use"::"
, otherwise a single":"
would have been left. Keep in mind with this method, it will only work with a single-nested namespace. Meaning"CommonCar::RedTrunk::BigWheels"
would have been transformed to"CommonCarRedTrunk::BigWheels"
.tr()
– uses the string parameters as arrays of single character replacments. In this case, because we're only replacing a single character, it'll work identically togsub()
. However, if you wanted to replace "on" with "EX", for example,gsub("on", "EX")
would produce"CommEXCar::RedTrunk"
whiletr("on", "EX")
would produce"CEmmEXCar::RedTruXk"
.
Docs:
https://apidock.com/ruby/String/gsub
https://apidock.com/ruby/String/sub
https://apidock.com/ruby/String/tr
how to safely replace all whitespaces with underscores with ruby?
The docs for tr! say
Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.
I think you'll get the correct results if you use tr without the exclamation.
Related Topics
Does Ruby Support Unicode and How Does It Work
How to Determine Leap Year in Ruby
Is There a Ruby Http Client Library with a Response Cache
Run a Cli Thor App Without Arguments or Task Name
How to Make a Ruby Script Run Once a Second
Gem Install Mongrel Fails with Ruby 1.9.1
Ruby: Merge Two Hash as One and with Value Connected
When and Where Do I Require Files in a Rails Application
Dealing with Large CSV Files (20G) in Ruby
What Rails Plugins Are Good, Stable and *Really* Enhance Your Code
Does Begin . . . End While Denote a 'Block'
How to Create Temp Dir in Ruby
Get Time Object at Start of Day in a Particular Time Zone
Add a Callback Function to a Ruby Array to Do Something When an Element Is Added