Renaming an array created by Array.new [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When creating a new array in IRB, I can use Array.new to get an empty, unassigned array.
Is there a way to reassign that new array? Can I turn, [] into a variable named my_new_array?
I know I can do this:
my_new_array = Array.new
Or I can do:
my_other_new_array = []
But what about reassigning Array.new?
I'm new to Ruby and I'm curious about this little nuance.

In IRB, the underscore _ method will give you the results of the last expression:
Array.new
# => []
my_new_array = _
# => []
Array.new(2, "foo")
# => ["foo", "foo"]
my_new_array = _
# => ["foo", "foo"]

Related

create nested hash from a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have got a list l = [:foo, :bar, :baz] and want to assign a varible into a hash h ={} programmatically.
Hash should look like
{ foo: { bar: { baz: some_value } } }
Note: the keys are variables!
Question:
How can I do this?
You could use inject on the reversed list :
l = [:foo, :bar, :baz]
h = l.reverse.inject(:some_value) do |value, key|
{ key => value }
end
p h
# {:foo=>{:bar=>{:baz=>:some_value}}}
reverse is used in order to build the innermost hash first, and keep building the nested hash outwards.

Ruby : How to take the next element of a array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got this tab :
["aaaaaaaaaaaaaaaaaaa",
"15/87/2014r",
"2453/NRc05",
"xxxxxxxxxxxxxxxxxxxxxxxxxx",
"Adaptée",
"09/12/2013",
"pub.pdf"]
And I only want "xxxxxxxxxxxxx" for example.
I found .next.element(s) but I got no idead of how to use it.. :/
Array#each returns an Enumerator:
arr = [1, 2, 3]
enum = arr.each
enum.next
#=> 1
enum.next
#=> 2
enum.next
#=> 3
enum.next
#=> StopIteration: iteration reached an end
Update
Regarding your comment
I have a array with some datas, and I wanted to save them in a hash with names like... {Name : aaaaa, First Name : bbbbbb} etc etc etc
Rather than calling next over and over again (I assume you are doing something like this):
data = ["John", "Doe"]
enum = data.each
hash = {}
hash[:first_name] = enum.next
hash[:last_name] = enum.next
# ...
You can combine two arrays with Array#zip and convert it to a hash using Array#to_h:
data = ["John", "Doe"]
keys = [:first_name, :last_name, :other]
keys.zip(data).to_h
#=> {:first_name=>"John", :last_name=>"Doe", :other=>nil}

How to iterate through this array within an array to see if all values are equal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is my sample code
:key1 => "a"
:key2 => "b"
:key3 => "c"
array1 = [[:key1, :key1, :key1],[:key1, :key2, :key3],[:key2, :key2, :key1]]
array1.each { |x| if x.sym_tos == "a"
puts "All match!"
else
puts "no match"
end
}
Yet when I run it, I get the following error code:
undefined method `sym_to_s' for [:R1C1, :R1C2, :R1C3]:Array (NoMethodError)
You probably wanted to say
if x.uniq.length == 1

How to make a copy of an array and manipulate its element in ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this array which I would like to copy and change an element's value. How can I do it (Ruby 1.9.3p429)
a = Array.new(2,"test") #a => ["test","test"] #a.object_id => 21519600 #a[0].object_id => 21519612
b = a.clone #b => ["test","test"] #b.object_id => 22940520 #b[0].object_id => 21519612
c = a.dup #c => ["test","test"] #c.object_id => 22865176 #c[0].object_id => 21519612
d = Array.new(a) #d => ["test","test"] #c.object_id => 23179224 #d[0].object_id => 21519612
c[0].upcase! #produces #a => ["TEST","TEST"], #b => ["TEST","TEST"], #c => ["TEST","TEST"] ...`
In Ruby every object is actually a reference to object so if you have array
x = [a, b, c, d]
and copy it into another array
y = x.clone
it will copy references to original objects, not objects themselves.
To do exactly what you want you would have to copy objects in a loop, however you're too focused on how you want to achieve array copying, instead of achieving your ultimate goal, get a new array which consists of upcased items of the original array.
Explore the Enumerable module and you will find things like #map, #select, #inject, etc. For instance this is how you get a copy of array with all names upcased:
["test", "test"].map { |element| element.upcase }
From your comment, you seem to want to upcase "c[0] only". I don't understand why you need to capitalize via a duplicate of a, but here is how to do it.
a = Array.new(2){"test"}
c = a.dup
c[0].upcase!
a # => ["TEST", "test"]

How to collect different data from array or hash by ruby language? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example,I have datas:
a,b,c,a,c,d
I want to get different datas: a,b,c,d
I want to get data count: a => 2,b => 1,c => 2,d => 1
There may have other datas,such as e,f,g,etc.
How to do?
a = [:a,:b,:c,:a,:c,:d]
p h = a.each_with_object(Hash.new(0)){|i,h| h[i] += 1}
p h.keys
# >> {:a=>2, :b=>1, :c=>2, :d=>1}
# >> [:a, :b, :c, :d]

Resources