Downcase string in array ruby [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 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.

Related

How can I produce one whole string from iteration on array of arrays 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 array of arrays looking something like this :
arr = [[f,f,f,f,f], [f,f,t,f,f], [f,t,f,t,f]]
and am I outputing it formatted on the console like this:
arr.each {|a| puts a.join.gsub('t','<b></b>').gsub('f','<i></i>')}
and it generates something like this:
<i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><b></b><i></i><i></i>
<i></i><b></b><i></i><b></b><i></i>
but it is only in the output. I am wondering how I can assign it to a string? With the new lines and everything, exactly the way it looks,
a= [["f","f","f","f","f"], ["f","f","t","f","f"], ["f","t","f","t","f"]].map do |arr|
arr.join.gsub(/[ft]/) do |x|
if x =~ /f/
'<i></i>'
elsif x =~ /t/
'<b></b>'
end
end
end.join("\n")
puts a
# >> <i></i><i></i><i></i><i></i><i></i>
# >> <i></i><i></i><b></b><i></i><i></i>
# >> <i></i><b></b><i></i><b></b><i></i>

Ruby: Convert a string into 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 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]

Iterating through a bignum - 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 8 years ago.
Improve this question
I have a 309 digit integer, I want to iterate through its characters.
Currently I am using:
require 'openssl'
e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117
and I have:
e.times do |i|
...
end
Which, understandably, yields an error:
undefined method `times' for #<OpenSSL::BN:0x007fec05002140>
I attempted to convert the bignum to an integer:
e.to_i.times do |i|
...
end
Which returned:
bignum too big to convert into `long'
I understand why I am receiving these errors, but I am asking how do I iterate through each character of such a large number?
How is this ?
e = 116505013962726356794269846667188147473899121100449069443844506823885859211073843523906823741034558875724969276233769835502344452366515593952571468651971447660633083078837371793388842846199643249996094940742465135064478448126948741186882484457847959126808512823416166517945252986434515406363102297514031583117
e.to_s.each_char do |c|
# code
end

Assign hash values in a method [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
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

How to use code block 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 8 years ago.
Improve this question
I have the following code
animals=['lion','tiger','zebra']
animals.each{|a| puts a}
I wanted to print only tiger in this array for that I wrote something like this
animals.each{|a| if a==1 puts animals[a]}
But it's not working why?
You can play with enumerable like this:
animals.select{ |a| a == 'tiger' }.each{ |a| puts a }
The wrong you did in your case:-
animals.each{|a| if animals[a]==2 puts a}
inline if statement you put in a wrong way.
#each passes element of the array,not the index. So animals[a] will not work. It will throw error as no implicit conversion of String into Integer (TypeError).
Do this as below using Array#each_index
animals=['lion','tiger','zebra']
animals.each_index{|a| puts animals[a] if animals[a] == 'tiger' }
# >> tiger
Maybe you are looking for this
animals.each_with_index{|animal, index| puts animal if index==1}
Please not that "tiger" occurs at index 1 and not 2.
you can simply do this
animals.fetch(animals.index('tiger')) if animals.include? 'tiger'
or
animals[animals.index('tiger')] if animals.include? 'tiger'

Resources