How do i reverse the element of this array using lambda ..
array = ["Hello","World"]
How do i reverse the elements in the array so that it is like this:
array = ["olleH","dlroW"]
array.map(&:reverse)
should do the trick
I don't see why would you use lambda for this but who am I to judge :)
lambda{|a| a.map(&:reverse)}.call(["Hello","World"].reverse)
Related
Persoane = []
Nume = gets
Persoane.push Nume.split(",")
puts Persoane.sort
I am trying to get an user to input carachters that get split into substrings which get inserted in an array, then the program would output the strings in alphabetical order. It doesnt seem to work and I just get the array's contents, like so:
PS C:\Users\Lenovo\Desktop\Ruby> ruby "c:\Users\Lenovo\Desktop\Ruby\ruby-test.rb"
Scrie numele la persoane
Andrei,Codrin,Bradea
Andrei
Codrin
Bradea
PS C:\Users\Lenovo\Desktop\Ruby>
you can do this :
Nume = gets
puts Nume.split(",").sort
or in 1 line
array = gets.chomp.split(",").sort
The error is because of your use of push. Let's assume that you define the constant Nume by
Nume='Andrei,Codrin,Bradea'
Then, Nume.split(',') would return the Array ['Andrei', 'Codrin', 'Bradea']. When you do a Persoane.push, the whole array is added to your array Persoane as a single element. Therefore, Persoane contains only one Element, as you can verify when you do a
p Persoane
If you sort a one-element array, the result will also be just that one element - there is nothing to sort.
What you can do is using concat instead of push. This would result in Persoane being a 3-element array which can be sorted.
I'm not sure you need use constants here
If you don't need keep user input and use it somewhere, you can just chain methods like this
persons = gets.chomp.split(",").sort
For something a little different, let's not split at all.
people = gets.scan(/[^,]+/).map(&:strip).sort
This will avoid problems like multiple commas in a row yielding empty strings. Of course, you could also avoid that with:
people = gets.split(/\,+/).map(&:strip).sort
I have a method which returns the number of hotels from a webpage:
hotel_count = self.getHotelsList.values
The output of this method is:
[["hotel_0", "hotel_1", "hotel_2", "hotel_3", "hotel_4", "hotel_5", "hotel_6", "hotel_7", "hotel_8", "hotel_9", "hotel_10", "hotel_11", "hotel_12", "hotel_13", "hotel_14", "hotel_15", "hotel_16", "hotel_17", "hotel_18", "hotel_19", "hotel_20", "hotel_21", "hotel_22", "hotel_23", "hotel_24", "hotel_25", "hotel_26", "hotel_27", "hotel_28", "hotel_29", "hotel_30", "hotel_31", "hotel_32", "hotel_33", "hotel_34", "hotel_35", "hotel_36", "hotel_37", "hotel_38", "hotel_39", "hotel_40"]]
I want to know the length of this array, but if I write
hotel_count = self.getHotelsList.values.length
The length is 1. How can I get a length of 41, which is the one I'm expecting?
Thanks
The array you are showing is nested inside another array. So the outer array is of length 1, the inner array is what you want.
To get it you have to first get the first element of the outer array using [0] or first
testList[0].length
testList.first.length
I am not sure why your getHotelsList method returns a nested array, it doesn't appear to need it.
hotel_count = getHotelsList.values.first.length
You can also do it with [0], but first is faster.
Two notes:
You don't need self at the beginning.
It is a bad habit to use camel case for method names in Ruby. it should better be get_hotels_list.
You could convert that into a single array with flatten:
hotel_count = self.getHotelsList.values.flatten.size
I have an array like this:
array = ["git-hw-abcd", "svn-hw", "svn-hw-design","git-hw"]
If I do array.sort I would basically get an ascending sort like this
["git-hw", "git-hw-abcd", "svn-hw", "svn-hw-design"]
Would like to sort the array by the values "svn-hw" and "git-hw" to appear as the first and second elements of the array so that I would get:
[ "svn-hw","git-hw",... then the rest of the values]
Any help would be appreciated.
Try this
* sorting on length(as sort criteria is not specified)
["git-hw-abcd", "svn-hw", "svn-hw-design","git-hw"].sort{|a,b| a.length <=> b.length}
It returns
["git-hw", "svn-hw", "git-hw-abcd", "svn-hw-design"]
array.sort{|a,b| a.split("-h")[1]<=>b.split("-h")[1]}
this is what you want
#Hivltg
try the length order using this :array= [ "svn-hw", "git-hw-abcd","git-hw", "svn-hw-design", "git-hw-bassics"]
you will find out the problem.
Not exactly sure how you're ordering, but here's an example of using a block to sort
array.sort{|a,b| a[4..-1] <=> b[4..-1]}
I am iterating on a array within a Object van. I am trying to pop the elements of the array into another object array. See below.
#van.bikes.each { #garage<<( #van.removebike )}
def removebike
#bikes.pop
end
When I do this the resulting array in the garage has missing elements and/or duplicate elements.
The reason for this is that when ruby iterates on the array it sets number of iterations based on the original array size. When you pop an element from that array the size changes so the iteration can not work properly.
You can use instead,
#van.bikes.count.times { #garage<<( #van.removebike )}
You can try this too..
#garage = []
#van.bikes.each{|bike| #garage << bike}
I need to add an array of array in to a single array without duplicates
array = [[1,2,3],[2,3,4],[7,8,9]]
to
new_array [1,2,3,4,7,8,9]
What is the best possible way to do IT in Ruby.
Try this:
array.flatten!.uniq!
flatten! takes any sub-arrays and adds their elements to the enclosing array (recursively), so it "flattens out" arrays of arrays.
uniq! removes duplicate elements from the array.
Note that ! methods modify the original array. Use the non-! methods (flatten and uniq) if you wish to return a new array instead.