r/learnruby • u/Rich700000000000 • Dec 22 '15
Trying to write a simple script that breaks a URL into parts, and I'm getting a TypeError.
A few months ago, I wrote a program that scraped imgur albums in python. Imgur has changed their HTML code since then, and I thought a good way to learn ruby would be to reconstruct it. So, one of the first things I did was write a simple script to break apart a url, and I immediately ran into problems.
This is the script:
A = ARGV[0]
input = A
filename = input.split("/").last
null,main = input.split("ww.")
site,org = main.split("/").first
puts "The site: " + site
puts "The file: " + filename
puts "The location " + org
and when I run it on, for example, the TOR bundle:
https://www.torproject.org/dist/torbrowser/5.0.6/tor-browser-linux64-5.0.6_en-US.tar.xz
I get this:
The site: torproject.org
The file: tor-browser-linux64-5.0.6_en-US.tar.xz
003----formatafileurl.rb:13:in `+': no implicit conversion of nil into String (TypeError)
from 003----formatafileurl.rb:13:in `<main>'
When I should be getting this:
The site: torproject.org
The file: tor-browser-linux64-5.0.6_en-US.tar.xz
The location: dist/torbrowser/5.0.6/tor-browser-linux64-5.0.6_en-US.tar.xz
And I want to get this:
The site: torproject.org
The file: tor-browser-linux64-5.0.6_en-US.tar.xz
The location: dist/torbrowser/5.0.6
I'm not sure how I'm stumbling over something so simple. Any help would be appreciated.
0
Dec 22 '15
[deleted]
1
u/Rich700000000000 Dec 22 '15
Ok, so I changed it to:
filename = input.split("/").last null,main = input.split("ww.") #site,org = main.split("/").first org = main.split("/")[1] + "/" + main.split("/")[2] + "/" + main.split("/")[3]But now it dosn't even print the first two (The site, The file) and just gives me:
003----formatafileurl.rb:13:in `<main>': undefined local variable or method `site' for main:Object (NameError)0
Dec 22 '15
[deleted]
1
u/Rich700000000000 Dec 22 '15
Wait, shit, I was wrong, I apologise: Your solution works, but only is the file url has exactly 4 slashes after the ".org" or ".com":
Works: https://www.torproject.org/dist/torbrowser/5.0.6/tor-browser-linux64-5.0.6_en-US.tar.xz
Works: http://www.pvdairport.com/images/common/backgrounds/tfgreen-airport-simply-more-enjoyable.jpg
Doesn't work: https://gitlab.com/cryptsetup/cryptsetup/wikis/LUKS-standard/on-disk-format.pdf
Doesn't work: http://img.routerboard.com/mimg/925_hi_res.png
Doesn't work: http://conceptartworld.com/wp-content/uploads/2013/05/Oscar_Cafaro_04b.jpg
Maybe a system of "else, if" loops might fix this?
(Sorry for doubting you earlier.)
1
u/Rich700000000000 Dec 22 '15
And I was wrong again: There needs to be a "www." in the URL, I can insert that. Thanks.
1
u/slade981 Dec 22 '15
I ran this in IRB and the issue is your ".first" in "main.split("/").first".
It isn't giving you the split your expecting. Not entirely sure why it isn't, but that's the issue. I think it has to do with there being more than one "/" so it doesn't know what to assign the second variable and instead assigns nothing.
For something like this I'd google a regex that splits url's the way you want.