Question

Can't understand what the issue with this

nameTag = "div[class='designer_about'] a"
designsTag = "li[class='span-2']"
pullTags = Array.new(nameTag, designsTag)

Error:

designers_list_mirraw.rb:8:in `initialize': can't convert String into Integer (TypeError)
from designers_list_mirraw.rb:8:in `new'
from designers_list_mirraw.rb:8:in `<main>'

I am new to ruby

Was it helpful?

Solution

As juanpastas mentions, Array::new(size,obj) expects a number and an object. You can see this in the tutorial you linked to:

names = Array.new(4, "mac")

names is now an array with "mac" four times.

If your intention is to create an array with these two items:

pullTags = Array.[nameTag, designsTag]
# which is equivalent to
pullTags = Array[nameTag, designsTag]
# which are the more verbose versions of
pullTags = [nameTag, designsTag]

See [](*args).

OTHER TIPS

Ary.new(length, item) is for the time you need an array have same item duplicate for certain times. It's not Array.new(item, item). The first parameter represent the length of array you want.

In your case you should use:

name_tag = "div[class='designer_about'] a"
designs_tag = "li[class='span-2']"
pull_tags = [name_tag, designs_tag]

BTW, by Ruby's convention, snake_case_variable_name is more preferred then camelCaseVariabelName

See Array::new

It receives integer and then an object, or an Array too, so you could use:

Array.new(2,'some') => ['some','some']
Array.new(an_array_here)

Ways to Build Arrays

The confusion comes from the fact that there are many ways to build arrays in Ruby. A non-exhaustive list includes:

  1. Calling Kernel#Array.
  2. Calling Array#new.
  3. Construct an Array with an array literal, e.g. [].

The semantics of each vary. The way you're doing it has the following signature:

Array#new(size=0, obj=nil)

which means the first argument is the size of the array, not an array element itself. Use a different invocation to achieve your desired results.

see the Array

nameTag = "div[class='designer_about'] a"
designsTag = "li[class='span-2']"

The new method can be called in this ways:

Array.new # => []
Array.new(2) # => [nil,nil]
Array.new(2,designsTag) # =>["li[class='span-2']", "li[class='span-2']"]

Or to add elements one by one you can use the push method:

[ 1, 2 ] << "c" << "d" << [ 3, 4 ] #=> [ 1, 2, "c", "d", [ 3, 4 ] ]

If you have string items, you can also use %W

> nameTag = "div[class='designer_about'] a"
# => "div[class='designer_about'] a" 
> designsTag = "li[class='span-2']"
# => "li[class='span-2']"
> %W(#{nameTag} #{designsTag}) 
# => ["div[class='designer_about'] a", "li[class='span-2']"]

Reference : http://www.zenspider.com/Languages/Ruby/QuickRef.html#arrays

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top