Does rails have an opposite of `parameterize` for strings? - ruby-on-rails

I used parameterize method. I want to de-parameterize it. Is there a method to do the opposite of parameterize?

No, there is not. parameterize is a lossy conversion, you can't convert it back.
Here's an example. When you convert
My Awesome Pizza
into
my-awesome-pizza
you have no idea if the original string was
My Awesome Pizza
MY AWESOME PIZZA
etc. This is a simple example. However, as you can see from the source code, certain characters are stripped or converted into a separator (e.g. commas) and you will not be able to recover them.
If you just want an approximate conversion, then simply convert the dashes into spaces, trim multiple spaces and apply an appropriate case conversion.

In Rails there is titleize (source):
"this-is-my-parameterized-string".titleize
=> "This Is My Parameterized String"
"hello-world foo bar".titleize
=> "Hello World Foo Bar"
As mentioned above, this isn't going to revert the string to its pre-parameterized form, but if that's not a concern, this might help!

I'm with Simone on this one but you can always go with
def deparametrize(str)
str.split("-").join(" ").humanize
end
:)

Related

Outsheet to quote and comma delimited text

My variables in Stata are of the form:
First Name: Allen
Last Name: Von Schmidt
Birth Year: 1965
County: Cape May
State: New Jersey
First Name: Lee Roy
Last Name: McBride
Birth Year: 1967
County: Cook
State: Illinois
I would like to outsheet them to create quote and comma separated rows in a .txt as:
"Allen,"Von Schmidt","1965","Cape May","New Jersey"
"Lee Roy","McBride","1967","Cook","Illinois"
How can I use outsheet (or another command) to do this? Do I need to make the numerics into strings first? Do I need to add a commas to each variable first?
I have tried the following:
outsheet first last birth_year county state using FileName.txt, nolabel delim(",")
This seems to work ok except that it does not put the numeric variables inside "".
I don't understand why you want this, but Stata's practice here as elsewhere is that only strings are placed in double quotes. So, to output numeric variables as if they were strings you do need to convert them first to string variables. The tostring command is designed for this.
But this is an awkward thing to do, and on the whole a bad idea.
First, and easier: if you use tostring you change your data, and numeric operations become impossible on the new string variables. That is relatively easy to work around. Just make sure you save your data first before using tostring and then read it back in again after exporting the data. Or use preserve followed by restore.
Second, and more problematic: you need to worry about loss of detail for any numeric variables that are not integer. tostring does have options that help here, but there are no guarantees of keeping every bit unless you get into nightmare territory of exporting hexadecimal. That's true of outsheet any way, but a warning should do no harm.
I am aware of the history of tostring, as its original author. I'll put on record that although it is a solution for what you appear to want to do, there are pitfalls as above and I don't recommend this way of working.
It would be better to explain why you think you need to do this. outsheet's export of numerics and strings seems to have worked well for export to other software, not least spreadsheets, over many uses.
P.S. as emphasised elsewhere, Stata does not regard " " as separators. They are delimiters for strings, but not separators for fields (or words in Stata's sense).

Is there a faster way to parse hashtags than using Regular Expressions?

I am curious, is there a faster/better way to parse hashtags in a string, other than using Regular Expressions (mainly in Ruby)?
Edit
For example I want to parse the string This is a #hashtag, and this is #another one! and get the words #hashtag and #another. I am using #\S+ for my regex.
You don't show any code (which you should have) so we're guessing how you are using your regex.
#\S+ is as good of a pattern as you'll need, but scan is probably the best way to retrieve all occurrences in the string.
'This is a #hashtag, and this is #another one!'.scan(/#\S+/)
=> ["#hashtag,", "#another"]
Its should be /\B#\w+/, if you don't want to parse commas
Yes, I agree. /\B#\w+/ makes more sense.
Maybe
Hmm, ideas....
You could try s.split('#'), and then perhaps apply a regex only to actual hashtags
s.split('#').drop(1).map { |x| x[/\w+/] } --- it may or may not be any faster but it clearly is uglier
You could write a C extension that extracts hashtags
You could profile your program and see if it really needs any optimization for this case.

Is there any advantage to _ever_ using a single quote around a string in Ruby/Rails?

I understand the functional difference between single and double quotes in Ruby, but I'm wondering what concrete reasons people have for varying between the two. In my mind it seems like you should just always use a double quote, and not think about it.
A couple rationales that I've read in researching the topic...
Use a single quote unless a double quote is required.
There's a very, very minor performance advantage to a single quotes.
Any other interesting thoughts out there? (Or maybe this is a case of the freedom or Ruby leaving the door open for no One Right Way to do something...)
I usually follow the following rule:
never use double quotes (or %Q or %W) if you don't interpolate
The reason for this is that if you're trying to track down an error or a security bug, you immediately know when looking at the beginning of the string that there cannot possibly any code inside it, therefore the bug cannot be in there.
However, I also follow the following exception to the rule:
use double quotes if they make the code more readable
I.e. I prefer
"It's time"
over
'It\'s time'
%q{It's time}
It is technically true that single quoted strings are infinitesimally faster to parse than double quoted strings, but that's irrelevant because
the program only gets parsed once, during startup, there is no difference in runtime performance
the performance difference really is extremely small
the time taken to parse strings is irrelevant compared to the time taken to parse some of Ruby's crazier syntax
So, the answer to your question is: Yes, there is an advantage, namely that you can spot right away whether or not a string may contain code.
I can think of three reasons to use single quoted strings:
They look cleaner (the reason I use them)
They make it easier to create a string you'd otherwise have to escape ('he said "yes"' vs "he said \"yes\"")
They are slightly more performant.
I would assume using a single-quoted string is faster, since double quotes allow string interpolation, and single-quoted strings do not.
That's the only difference I know of. For that reason, it's probably best to only use a single-quoted string unless you need string interpolation:
num = 59
"I ate #{num} pineapples"`
Well, there are a lot of fuzz about the "performance gain" of single quoted strings vs double quoted strings.
The fact is that it doesn't really matter if you don't interpolate. There are a lot of benchmarks around the web that corroborate that assertion. (Some here at stackoverflow)
Personally, I use double for strings that have interpolation just for the sake of readability. I prefer to see the double quotes when I need them. But in fact there are methods in ruby for interpolating strings other than "double quoting" them:
%q{#{this} doesn't get interpolated}
%Q{#{this} is interpolated}
1.9.2-p290 :004 > x = 3
=> 3
1.9.2-p290 :005 > "#{x}"
=> "3"
1.9.2-p290 :006 > '#{x}'
=> "\#{x}"
In any other case, i prefer single quotes, because it's easier to type and just makes the code less overbloated to my eyes.
Since asking this question I've discovered this unofficial Ruby Style Guide that addresses this, and many many more styling questions I've had floating around in my head. I'd highly recommend checking it out.
I found that when putting variables in a string using #{} did not work in single quotes, but did work in double quotes as below.
comp_filnam and num (integer) are the variables I used to create the file name in the file path:
file_path_1 = "C:/CompanyData/Components/#{comp_filnam}#{num.to_s}.skp"

Regex with ruby issue

My regular extression (regex) is still work in progress, and I'm having the following issue with trying to extract some anchor text from a hash of where the element is stored.
My hash looks like:
hash["example"]
=> " Project, Area 1"
My ruby of which is trying to do the extraction of "Project" and "Area 1":
hash["ITA Area"].scan(/<a href=\"(.*)\">(.*)<\/a>/)
Any help would be much appreciated as always.
Your groups are using greedy matching, so it's going to grab as much as it can before, say, a < for the second group. Change the (.*) parts to (.*?) to use possessive matching.
There are loads of posts here on why you should not be using regex to parse html. There are many reasons why... such as, what if there is more than one space between the a and href, etc. It would be ideal to use a tool designed for parsing html.
You will have to exape the backslashes for the backslashes. so something like... \\\\ instead of just \\. It sounds stupid, but I had a similar problem with it.
I'm not entirely sure what your issue is, but the regexp should match. Double quotes " need not be escaped. As mentioned in Dan Breen's answer, you need to use non-greedy matchers if the string is expected to contain more than one possible match.
The canonical SO reason to use a real HTML parser is calmly explained right here.
However, regexen can parse simple snippets without too much trouble.
Update: Aha, the anchor text. That's actually pretty easy:
> s.scan /([^<>]*)<\/a>/
=> [["Project"], ["Area 1"]]

better alternative in letters substitution

Is there any better alternative to this?
name.gsub('è','e').gsub('à','a').gsub('ò','o').gsub('ì','i').gsub('ù','u')
thanks
Use tr.
Maybe like string.tr('èàòìù', 'eaoiu').
substitutes = {'è'=>'e', 'à'=>'a', 'ò'=>'o', 'ì'=>'i', 'ù'=>'u'}
substitutes.each do |old, new|
name.gsub!(old, new)
end
Or you could use an extension of String such as this one to do it for you.
If you really want a full solution, try pulling the tables from Perl's Unidecode module. After translating those tables to Ruby, you'll want to loop over each character of the input, substituting the table's value for that character.
Taking a wild stab in the dark, but if you're trying to remove the accented characters because you're using a legacy text encoding format you should look at Iconv.
An introduction which is great on the subject: http://blog.grayproductions.net/articles/encoding_conversion_with_iconv
In case you are wondering the technical terms for what you want to do is Case Folding and possibly Unicode Normalization (and sometimes collation).
Here is a case folding configuration for ThinkingSphinx to give you an idea of how many characters you need to worry about.
If JRuby is an option, see the answer to my question:
How do I detect unicode characters in a Java string?
It deals with removing accents from letters, using a Normalizer. You could access that class from JRuby.

Resources