How to Return Everything After Last Slash(/) in a Ruby String

How can you return everything after last slash(/) in a Ruby string

I don't think a regex is a good idea, seeing how simple the task is:

irb(main):001:0> s = 'https://www.facebook.com/hackerbob'
=> "https://www.facebook.com/hackerbob"
irb(main):002:0> s.split('/')[-1]
=> "hackerbob"

Of course you could also do it using regex, but it's a lot less readable:

irb(main):003:0> s[/([^\/]+)$/]
=> "hackerbob"

ruby get everything after last

You can use Ruby's String#split() method

irb(main):001:0> string="foo.tar.gz"
=> "foo.tar.gz"
irb(main):002:0> string.split(".")[-1]
=> "gz"

How to gsub slash (/) to in ruby

I believe what you may be asking is "how do I force Ruby to evaluate string interpolation when the interpolation pattern has been escaped?" In that case, you can do this:

eval("\"#{ss}\"")

If this is what you are attempting to do, though, I would highly discourage you. You should not store strings containing the literal characters #{ } in your database fields. Instead, use %s and then sprintf the values into them:

# Stored db value
ss = "http://url.com/?code=%s"

# Replace `%s` with value of `code` variable
result = sprintf(ss, code)

If you only need to know how to remove \ from your string, though, you can represent a \ in a String or Regexp literal by escaping it with another \.

ss.gsub(/\\/,'')

Remove everything after some characters

This can be relatively easily accomplished by running split("More info") on your strings. What that does is breaks the string in to an array like so:

new_string  = "Posted today More info Go to Last Post"
new_string = new_string.split("More info")
# becomes ["Posted today ", " Go to Last Post"]

What split does is it breaks a string apart in to an array, where each element is what preceded the argument. So if you have "1,2,3" then split(",") will return [1, 2, 3]

So to continue your solution, you can get the posting date like this:

new_string[0].strip

.strip removes spaces at the front or back of a string, so you'll be left with just "Posted today"

Use a string with forward slashes as a single parameter in routes.rb

You can use wildcards in your routes that will match forward slashes. Try something like this:

"/comatose_admin/*full_path"

Then params[:full_path] should contain the rest of the request path.

See Route Globbing

How to access to the company name into an email in rails?

I'm not an expert on rails, but you can try this :

@pars1 = @company.split('@')
@pars2 = @pars[1].split('.')
@pars3 = @pars[0]
=> company

Try to read that, it can be useful :

How can you return everything after last slash(/) in a Ruby string

get value after last slash (/)

You can use lastIndexOf and substring:

var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");

or using .split() and .pop()

var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");

Get substring after the first = symbol in Ruby

Not exactly .after, but quite close to:

string.partition('=').last

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition

Selecting a string until the first period after 100 characters

Use a regexp

string[/\A.{100,}?\./]

How to split a string by slash in ruby

Problem

Your tmp variable is enclosed in double-quotes, and contains a backslash which is being interpreted as an escape rather than a character literal. You can see this easily by simply pasting your string into a REPL like irb:

"DESKTOP-AHDESI\Username" #=> "DESKTOP-AHDESIUsername"

You need to handle the backslash specially in both your String methods.

Solution

One way to handle this is to use Ruby's alternate quoting mechanism. For example:

%q(DESKTOP-AHDESI\Username).split '\\'   #=> ["DESKTOP-AHDESI", "Username"]

This may not help you directly, though.
Wherever the value from tmp is coming from, you need to refactor the code to ensure that your String is properly escaped before you assign it to your variable, or otherwise pre-process it. String#dump won't really help much if the value you're assigning is unescaped before assignment, so you're going to have to fix this in whatever code you're using to generate or grab the string in the first place.



Related Topics



Leave a reply



Submit