2

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

3
  • 2
    Don't use Array.new. Use the ['array', 'literal']. Could this be the cause? Commented Jul 2, 2013 at 16:52
  • @JanDvorak thanks. It helped. But I saw the above method outhere tutorialspoint.com/ruby/ruby_arrays.htm Commented Jul 2, 2013 at 16:54
  • If your question has been answered you should accept the one you found most helpful. Commented Jan 11, 2014 at 22:45

6 Answers 6

25
+300

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).

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

3

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)

Comments

2

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.

Comments

1

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 ] ]

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.