Cannot manipulate my results

require “http”

require “xml”

xml = HTTP::Client.get(“Balloon - Wikipedia”).body

document = XML.parse(xml)

document.xpath_nodes("//a").each do |link|
puts link[“href”]?

This is the source code to my simple web crawler, it works with the exception of when I want to modify the results of it in anyway it gives me an error when I try to run it. I’ll give an example, I tried to put the results of the “puts link[“href”]?” inside of a variable, so that I could check them to see if they are full URL’s. When I do that though I get an error, how can I fix this?

Can you share the error you’re getting? Otherwise it’ll be kinda hard to help.

Also if it would be easier for you, there are a few chat rooms for Crystal that may provide faster help than the forums: Community - The Crystal Programming Language.

var = (document.xpath_nodes("//a").each do |link|
puts link[“href”]?

finished =

puts finished

This is the error below:
Showing last frame. Use --error-trace for full trace.


22 | finished =
Error: undefined method ‘split’ for Nil

Also I didn’t know there was a chat room, thanks!

Okay few things going on here.

  1. #each returns nil, unlike in Ruby
  2. .map(&:first) isn’t valid Crystal, you’d want .map(&.first)
  3. Related to 1, you need to properly handle nil now, unlike Ruby.

Some links that may help with this:

P.S. You can wrap your code in triple backticks, it makes it look better. E.g.

var = (document.xpath_nodes("//a").each do |link|
puts link[“href”]?

I knew I would find something that isn’t really the same as ruby after a while. I guess it’s really time to learn.

1 Like

Yes, it should be made clear that Crystal is NOT just “compiled Ruby.” While they do share similar syntax, they are two completely different languages. If existing Ruby code works in Crystal great, but compatibility with it is not a goal.

1 Like