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

Related

How to get args when call function with square brackets in the 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 3 years ago.
Improve this question
I am the beginner of Ruby.
I get one problem when I read the Ruby code.
There have one function with square-brackets.
How can I get the args from that function?
Here is the Class
class Student
class << self
def count
end
end
end
Here is the function request.
Student.count["Jack"]
Square brackets have no special meaning in Ruby. It is rather a method #[] called on the receiver.
Amongst many others, this method is noticeably declared by Array, Hash and Proc. Because of the parameter passed to #[], which is "Jack" string, it is most likely either Hash or Proc.
That said, it depends on what is returned by Student::count.
Hash example
def count
{"Jack" => 1, "Mary" => 2}
end
count["Jack"]
#⇒ 1
Proc example
def count
->(name) { "Hi, #{name}!" }
end
count["Jack"]
#⇒ "Hi, Jack!"

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

How to return a result composed of two changed has_and_belongs_to_many relations [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
r is an array that has three items that is the result of
r = Part.components.products.uniq
where Part HABTM Component and Component HABTM Product.
Why does this code:
class Array
def p_object_ids
puts each { object_id }.join(", ")
end
end
p r.class
r.p_object_ids
p r.count
generate this output:
Array
#<User:0x00000006535650>, #<User:0x000000065338f0>, #<User:0x000000065336e8>
1
It returns 1 when it's not really an array of three items, but an Array containing a single ActiveRecord. The correct implementation of what I was looking for turned out to be:
class Part
def products
Prooduct.joins(components: :part).where(parts: {id: self.id})
end
end
and not
self.components.map(&:products).uniq.to_a

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'

Is there a shortcut for assigning some variable with the return value of a method that's used on it 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 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"

Resources