call a method on variable - ruby-on-rails

I have a question, I have two methods and I would want to call on my second method the first one in a variable.
How is it possible?
Is it good what I did?
def ping_check_url(url)
check = Net::Ping::External.new(url)
check.ping?
end
def get_info_yml_file
config = YAML.load_file('config_url.yaml')
config.each do |key, value|
key = key
ping_check_url.(url = value['url_web_site'])
# ping_check_url(url)
puts " #{key} : #{#url} "
end
end

You should do
ping_check_url(value['url_web_site'])
instead of
ping_check_url.(url = value['url_web_site'])

Please try this
def ping_check_url(url)
check = Net::Ping::External.new(url)
check.ping?
end
def get_info_yml_file
config = YAML.load_file('config_url.yaml')
config.each do |key, value|
key = key
ping_check_url(value['url_web_site'])
# ping_check_url(url)
puts " #{key} : #{#url} "
end
end
Also, you are not initializing #url anywhere but using it in get_info_yml_file. it would always return nil

Related

Ruby how to modify parameters

so i have this code that and my aim was to convert any empty string to null
def convert_empty_strings_to_null
if request.patch? || request.post?
convert_empty_strings_to_null_rec(request.params)
end
end
def convert_empty_strings_to_null_rec(param)
param = nil if param.empty? if param.is_a?(String)
param.all?{|v| convert_empty_strings_to_null_rec v} if param.is_a?(Array)
param.all?{|k,v| convert_empty_strings_to_null_rec v} if param.is_a?(Hash)
end
But i'm new to ruby on rails and i found it that it sends params by value and not by reference, so no change in params is made, how do i fix this ?
I assume that by "empty" you mean zero-with strings, meaning that strings consisting only of whitespace should be left intact. (Otherwise blank? and strip would be your friends.)
def convert_empty_strings_to_nil
if request.patch? || request.post?
request.params.each do |key, value|
request.params[key] = convert_empty_strings_to_nil_rec(value)
end
end
end
def convert_empty_strings_to_nil_rec(param)
case param
when String
param.empty? ? nil : param
when Array
param.map{ |v| convert_empty_strings_to_nil_rec(v) }
when Hash
param.map{ |k,v| [k, convert_empty_strings_to_nil_rec(v)] }.to_h
else
param
end
end
First of all, this is how your convert_empty_strings_to_null_rec method should be, for keeping the changes persistent:
def convert_empty_strings_to_null_rec(param)
if param == ""
updated_param == nil
elsif param.is_a?(Array)
updated_param == param.map{|x| nil if x.empty? }
elsif param.is_a?(Hash)
updated_param = {}
param.each do |k, v|
if v.empty?
updated_param[k] = nil
else
updated_param[k] = v
end
end
end
return updated_param
end
Further, I am assuming from your question that convert_empty_strings_to_null is a action method. It should be updated to catch what convert_empty_strings_to_null_rec method is returning.
def convert_empty_strings_to_null
if request.patch? || request.post?
updated_params = convert_empty_strings_to_null_rec(request.params)
end
# you can use the updated_params here on in this action method
end
Hope it helps : )

Understanding tap in Ruby

I am reviewing a piece of code from a Rails project and I came across the tap method. What does it do?
Also, it would be great if someone could help me understand what the rest of the code does:
def self.properties_container_to_object properties_container
{}.tap do |obj|
obj['vid'] = properties_container['vid'] if properties_container['vid']
obj['canonical-vid'] = properties_container['canonical-vid'] if properties_container['canonical-vid']
properties_container['properties'].each_pair do |name, property_hash|
obj[name] = property_hash['value']
end
end
end
Thanks!
.tap is here to "perform operations on intermediate results within a chain of methods" (quoting ruby-doc).
In other words, object.tap allows you to manipulate object and to return it after the block:
{}.tap{ |hash| hash[:video] = 'Batmaaaaan' }
# => return the hash itself with the key/value video equal to 'Batmaaaaan'
So you can do stuff like this with .tap:
{}.tap{ |h| h[:video] = 'Batmaaan' }[:video]
# => returns "Batmaaan"
Which is equivalent to:
h = {}
h[:video] = 'Batmaaan'
return h[:video]
An even better example:
user = User.new.tap{ |u| u.generate_dependent_stuff }
# user is equal to the User's instance, not equal to the result of `u.generate_dependent_stuff`
Your code:
def self.properties_container_to_object(properties_container)
{}.tap do |obj|
obj['vid'] = properties_container['vid'] if properties_container['vid']
obj['canonical-vid'] = properties_container['canonical-vid'] if properties_container['canonical-vid']
properties_container['properties'].each_pair do |name, property_hash|
obj[name] = property_hash['value']
end
end
end
Is returning a Hash beeing filled in the .tap block
The long-version of your code would be:
def self.properties_container_to_object(properties_container)
hash = {}
hash['vid'] = properties_container['vid'] if properties_container['vid']
hash['canonical-vid'] = properties_container['canonical-vid'] if properties_container['canonical-vid']
properties_container['properties'].each_pair do |name, property_hash|
hash[name] = property_hash['value']
end
hash
end
Tap is a Ruby method from the Object class.
This method yields x to the block and then returns x. This method is used to "tap into" a method chain, to perform operations on intermediate results within the chain.

Clear all values in nested ruby hash

How can I remove all values from ruby has. I don't want to remove keys just values.
For example:
here is my hash: {'a'=>{'b'=>'c'},'d'=>'e','f'=>{'g'=>''}}
I want this: {'a'=>{'b'=>nil},'d'=>nil,'f'=>{'g'=>nil}}
I don't want to delete the nested hashes. The nesting level varies from one to six levels
thanx
You can write custom delete_values! method, like this:
class Hash
def delete_values!
each_key do |key|
self[key].is_a?(Hash) ? self[key].delete_values! : self[key] = nil
end
end
end
{'a'=>{'b'=>'c'},'d'=>'e','f'=>{'g'=>''}}.delete_values!
# => {"a"=>{"b"=>nil}, "d"=>nil, "f"=>{"g"=>nil}}
h = {'a'=>{'b'=>'c'},'d'=>'e','f'=>{'g'=>''}}
def clean_hash h
h.each do |key, value|
if value.instance_of? Hash
clean_hash value
else
h[key] = nil
end
end
end
clean_hash h
#{"a"=>{"b"=>nil}, "d"=>nil, "f"=>{"g"=>nil}}
h = {'a'=>{'b'=>'c'},'d'=>'e','f'=>{'g'=>''}}
def cleaned_hash(h)
h.reduce({}) do |memo, (key, val)|
memo[key] = if val.is_a? Hash
cleaned_hash(val)
else
nil
end
memo
end
end
cleaned_hash h
# => {"a"=>{"b"=>nil}, "d"=>nil, "f"=>{"g"=>nil}}
This will not modify your hash but instead give you cleaned copy

Ruby: How to make custom DSL accept variables

I have this class:
class Items
def initialize &block
(block.arity < 1 ? (instance_eval &block) : block.call(self)) if block_given?
end
def button_id button_id=nil
unless #button_id.present?
raise "button_id must be supplied" if button_id.nil?
#button_id = button_id
end
#button_id
end
end
Now, when I do this it works:
Items.new do
button_id 1
end
But when I do this, it fails because I think it is not on the same scope:
#button = Button.find(params[:button_id]
Items.new do
button_id #button.id
end
How can fix this to take arguments outside the scope?
Thanks!
Try this:
class Items
def self.dsl
new.tap do |item|
yield item
end
end
def button_id(button_id)
#button_id = button_id
end
end
#button = Button.find(params[:button_id])
item = Items.dsl do |item|
item.button_id(#button.id)
end
puts item.inspect
Turns out all I needed to do was to pass the arguments to the block like this:
Items.new do |item|
item.button_id #button.id
end
Less beautiful DSL but works.
I don't think this is the right use case of DSL, when you can simply assign the attributes by arguments.
class Item
attr_accessor: :button_id
def initialize(args)
button_id = args[:button_id]
end
end
Another problem is in your usage. The instance would be of little value if you don't assign it to a variable
item = Item.new button_id: button_id

Trying to perform a method on a params in Rails

I want to write a method that loops through all the params to make sure they aren't all blank.
My params are:
params[:search][:company]
params[:search][:phone]
params[:search][:city]
params[:search][:state]
params[:search][:email]
I have this method:
def all_blank_check(params)
array=[]
params[:search].each do |key, value|
array << value unless value.blank?
end
if array.count < 1
return true
else
return false
end
end
But when I try something like all_blank_check(params) I get the following error:
NoMethodError (undefined method `all_blank_check' for #<Class:0x108c08830>):
Do I need to convert the params to an array first? Can't I perform a method on params?
Edit - full source:
def index
#customers = Customer.search_search(params)
end
def self.search_search(params)
search_field = []
search_values = []
array = []
test = ''
if !params[:search].nil? && all_blank_check(params[:search]
if !params[:search].nil? && !params[:search][:company].blank?
search_field << 'customers.company LIKE ?'
search_values << "%#{params[:search][:company]}%"
end
if !params[:search].nil? && !params[:search][:city].blank?
search_field << 'customers.city = ?'
search_values << "#{params[:search][:city]}"
end
if !params[:search].nil? && !params[:search][:phone].blank?
search_field << 'customers.phone_1 = ?'
search_values << "%#{params[:search][:phone]}%"
end
conditions = [search_field.join(' AND ')] + search_values
Customer.where(conditions).includes(:customer_contacts).limit(10)
end
end
def all_blank_check(params)
params[:search].each do |key, value|
array << value unless value.blank?
end
if array.count < 1
return false
end
if array.count > 1
return true
end
end
You can also use more Ruby-minded code like this:
def self.all_blank?(params)
params[:search].count{|key, value| !value.blank?} == 0
end
This counts the values that are not blank; if the number is 0, it means all are blank.
It avoids creating a new array just for counting.
The problem is not the type of params, the problem is that the method all_blank_check does not exist on the object you call it on.
You defined it as an instance method and you're trying to call it from the class method search_param, which won't work.
If you want to make all_blank_check a class method you need to change the definition to def self.all_blank_check(params) - same as search_param.

Resources