Sorry, I am just learning how to use Rails.
I've got a simple .txt file asset which I would like to pull random Strings from to display on my landing page.
Is there an easy way in Rails to do this?
Assuming each string is in a separate line, you can do this:
strings = File.readlines('path/to/file.txt')
Then, to get a random string use sample, like this:
strings.sample
If you wan't more than one random string, just use sample with an argument, for example:
strings.sample(3)
This will return an array with 3 random lines from strings array.
Finally, you can do all in one line, for example, try this in the controller:
#string = File.readlines('path/to/file.txt').sample
And you will have #string available to use in the view.
So you are not giving me much. but I am going to assume that you want to get 1 line of a text file.
This is how I would do it
File.readlines("my/file/path.txt").sample
I hope that get you started :)
Related
I want to use the output of {{Special:Editcount/User}} in #expr to calculate something. But of course the output is handled as a string, even if I split it into its figures with #sub. So how can I make it being recognized as int? Any idea?
Try {{formatnum|{{Special:Editcount/<user>}}|R}} (docs).
You cannot do that, because embedded special pages such as {{Special:Editcount}} are filled out at load time, after the Wikitext has already been parsed. In order to parse such content you will need runtime JavaScript.
My sample text looks like this:
30","formatedMinDeliveryDate":null,"formatedMaxDeliveryDate":null,"actualDeliveryDate":null,"trackingNumber":"ID180135116580CN","shippmentTrackingUrl":"https:\u002F\u002Fwww.057872.m2749.l5119","localizedCurrency":null}},"actions":[{"label":"Leave feedback","icon":null,"value":null,"action":"link","actionParam":{"label":"LEAVE_FEEDBACK_FOR_SELLER","u
I want to get the number ID180135116580CN and I'm having trouble achieving this using regexp.
The file is full of them and I'm doing this
out_file = File.open('public/orders.txt').each do |line|
p line[/(?<="trackingNumber":")[^"]*(?=")/]
end
but it only prints nil and doesn't extract the number I'm looking for.
Is the regexp wrong or do I need to traverse the file differently?
Basically after every trackingNumber, I want to get whatever is in quotes there after.
Thanks!
Edit:
Attempted this as per #WiktorStribiżew suggestion in the comments
p line.scan(/"trackingNumber"\s*:\s*"([^"]+)"/)
Now, I'm getting all of trackingNumbers as an array like this
[["UB08578YP"], ["UB085789YP"], ["ID180135791CN"], ["ID180135728CN"]]
How do I modify this to get them in individual lines like this?
UB08578YP
UB085789YP
ID180135791CN
ID180135728CN
it can be simpler if you read the file completely and run the scan there something like this
file_content = open("./my_file.txt").read
results = file_content.scan(/(?<="trackingNumber":")[^"]*(?=")/)
puts results
It will have them formated. in the way you want it.
I am just starting a very basic program in Grails (never used it before, but it seems to be very useful).
What I have so far is:
in X.groovy,
a String named parameters, with constraint of maximum length 50000 and a couple other strings and dates, etc.
in XController.groovy,
static scaffold = X;
It displays the scaffold UI (very handy!), and I can add parameter strings and the other objects associated with it.
My problem is that the parameters string is a long string with formatting that is pasted in by the user. When it is displayed on the browser, however, it does not retain any carriage returns.
What is the best way to go about this? I'm a very beginner at Grails and still have lots and lots of learning to do on this account. Thanks.
The problem is that the string is being displayed using HTML which doesn't parse \n into a new line by default. You need to wrap the text in <pre> (see: http://www.w3schools.com/tags/tag_pre.asp) or replace the \n with <br/> tags to display it correctly to the user.
I'm trying to piece together a part of a create action in a controller that scans the the text entered and intelligently understands what type of domain name it is.
I have a text box called "domain_names". A user puts domains into the box separated by commas, e.g. "yahoo.com, google.com"
In the controller it hits it like this:
#extracted_domains = (params[:domain_names]).split(",")
#extracted_domains.each do |domain|
domain.strip
domain_scan = domain.scan(/(\w+)[.]/).flatten
com_scan = domain.scan(/[.](\w+)/).flatten
new_domain_type = DomainType.find_or_create_by_domain_type(:domain_type => com_scan)
new_domain = Domain.create(:domain => domain_scan, :domain_type_id => new_domain_type.id)
end
In the console it works great. But when I put it into practise I get really odd things stored in the database. For example if :domain was meant to have the value "google", it will instead have the value "---\n- google\n" , when its stored in the database.
No idea why
Thanks in advance.
UPDATE**
Problem: It was putting an array into a string.
Solution: Make it a string.
domain_scan = domain.scan(/(\w+)[.]/).flatten.first
com_scan = domain.scan(/[.](\w+)/).flatten.first
It appears to be fed YAML input. Three dashes at the beginning of the string followed by a newline are a strong indicator of YAML: http://en.wikipedia.org/wiki/YAML#Sample_document
As for your issue, can we see the exact params that are sent?
I would take a look at https://github.com/pauldix/domainatrix for domain extraction.
I am getting text from a feed that has alot of characters like:
Insignia™ 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation Apple® iPod® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar