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 a stupid ruby question
def a(ok)
#...
ok
end
a("123")
a(["123","456"])
How can I make output as array?
["123"]
["123","456"]
Use the method Kernel#Array
def a(ok)
Array(ok)
end
a("123") # => ["123"]
a(["123","456"]) # => ["123", "456"]
Use Array#wrap
Array.wrap("123") # => ["123"]
Array.wrap(["123","456"]) # => ["123","456"]
Edit:
This is a rails extension, if you don't use rails, just omit this answer.
def a(a)
[a].flatten
end
a([1,2,3,4])
[1, 2, 3, 4]
a(1)
[1]
a([1,[2]])
[1, 2]
Related
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 4 years ago.
Improve this question
I have some strange thing with downcase and upcase my string in array. Share my code:
I suspect the issue is that you do not have correctly encoded strings.
foo = ['МеНше', '4.5']
foo.map(&:downcase) #=> ["менше", "4.5"]
foo.each { |el| puts el.downcase }
#>> менше
#>> 4.5
foo.first.encoding #=> #<Encoding:UTF-8>
The first step would be to check your encoding. If it's not UTF-8, you can coerce a downcase by doing:
foo.each { |el| puts el.mb_chars.downcase.to_s }
#>> менше
#>> 4.5
This solution requires Rails, so you'd need to do
require 'active_support/core_ext'
If you're using plain old 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 5 years ago.
Improve this question
I have a program where values in an array are [1,2,46,5,8,2,8], now I want to remove duplicate values but without using unique method of Ruby. Can anyone help with the logic of doing it? I want to make a unique array.
The code that I am using is may be not absolute to the question which is why I am getting the answer to first two situations but not the last one. Here is the code :
def uniq(array)
i=0
while i < array.length
if array[i] == array[i+1]
puts ""
else
puts i
end
i += 1
end
end
uniq([5,5,5,5])
uniq([1])
uniq([1,2,46,5,8,2,8])
Another way is to convert to a Set which will remove the duplicates, then convert back to an array. First require 'set' (in the Standard Lib) then, either of these:
Set.new([1,2,3,3]).to_a
# => [1, 2, 3]
# Does the same thing without passing the array to the Set constructor
[1,2,3,3].reduce(Set.new, &:add).to_a
# => [1, 2, 3]
You could use the intersection or union:
arr = [1,2,46,5,8,2,8]
# => [1,2,46,5,8,2,8]
unique_arr = arr & arr
# => [1, 2, 46, 5, 8]
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 6 years ago.
Improve this question
I am trying to create an any_strings? method that takes an array and returns true if there is a string in the array. For example:
a = [ 1, 2, 3, "string" ]
any_strings?(a)
#=> true
b = [ 1, 2, 3, 4 ]
any_strings?(b)
#=> false
Define any_strings? as follows:
def any_strings?(a)
end
Am I supposed to use the is_a? method, I am quite confused with how exactly you use 2 block methods
Shouldn't be too hard.
def any_strings?(array)
array.any? { |element| element.is_a?(String) }
end
This is rather easy to do in point-free style:
def any_strings?(array)
array.any?(&String.method(:===))
end
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 7 years ago.
Improve this question
If I have
footnotes = { "apple" => "is a fruit", "cat" => "is an animal", "car" => "a transport"}
How can I get the index of these?, something like:
footnotes["cat"].index
# => 1
There's no need to first retrieve the keys:
footnotes.find_index { |k,_| k== 'cat' } #=> 1
As to the question of whether hash key-value pairs have an index (since v1.9), I would just point out that the Ruby monks decided to supply us with Enumerable#find_index, which of course means Hash.instance_methods.include?(:find_index) #=> true.
You can do
footnotes.to_a.index {|key,| key == 'cat'}
# => 1
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
Say I have this method
def foo(bar1, bar2)
## code
end
How could I implement the code so that, when I call foo('hello', 'world'), the foo method accesses a hash, giving:
{
:bar1 => 'hello',
:bar2 => 'world'
}
Is there a Ruby (Rails?) built in method, or how could I write it?
def foo(bar1, bar2)
names = method(__method__).parameters.map{|e| e[1]}
Hash[names.zip(names.map {|name| eval(name)})]
end
Don't do that. It's ugly and evil. Give me the whole context, you're doing something wrong.
def foo(bar1, bar2)
{bar1: bar1, bar2: bar2}
end
If what Sergio mentions was the intention of the question, then
def foo(bar1, bar2)
Hash[method(__method__).parameters.map{|_, k| [k, eval(k.to_s)]}]
end