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'
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am new to ruby and trying out some examples.I have the array below that contains a hashmap.
f = [{"qty"=>"5", "unit"=>"kgs", "item"=>"sugar", "cost"=>"400", "salestax"=>"0.0"}]
I want to print out some thing like this
5 kgs of sugar : 400 at a tax of 0.0(if you notice the content is from the hashset)
I've tried some thing like:
f.each
{
|m| puts m for u in m |qty,unit,item,cost,salestax| puts "#{qty} #{unit} of #{item} : #{cost} #{salestax}"
}
but its not giving me what I want.
f.each do |hash|
puts "#{hash['qty']} #{hash['unit']} of #{hash['item']}: #{hash['cost']} at a tax of #{hash['salestax']}."
end
Seems like that is what you want.
Another way
f.each { |hash| puts "%s %s of %s : %s at a tax of %s" % hash.values }
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>
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
The next code search if keyword appear in hash values and print yes if so,
but it works well in codeacademy console, but in my Rubymine it give me exception
NoMethodError: undefined method `keys' for nil:NilClass
I've tried to use each_key method but it was the same rusult.
arr = [
{ name: "2222", num:"4444 kod"},
{ name: "3222 kod", num:"43423444"},
{ name: "224422", num:"4442424"}
]
p = "kod"
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
esle
puts "no"
end
end
Can you give some advice?)
You have 2 mistakes:
You wrote esle instead of else
You are missing one end clause
Your blocks need end keyword. And else should be spelt correctly.
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
else
puts "no"
end
end
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 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
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 often do the following:
#value = #value.some_method
But isn't there a shorter syntax for that in ruby? Some methods offer bang equivalents, but sadly not all...
For iterations one can use:
i += 1
Is that, or something similar, also available for my code snippet above?
There's nothing that does this in a shorter way.
To be fair, it is an unusual pattern, and while not especially rare, would lead to confusion if there was an operator like:
#value .= some_method
How is that even supposed to be parsed when reading?
As the Tin Man points out, in-place operators are really what are best here.
You cannot do that taking the actual variable as the receiver because there is no way to get to the name of the variable. Instead, you need to use the name of the variable like this:
class A
attr_accessor :value
def change_to_do_something name
instance_variable_set(name, "do something")
end
end
a = A.new
a.value = "Hello"
p a.value
# => "Hello"
a.change_to_do_something(:#value)
p a.value
# => "do something"