I have a simple function which takes a JSON and 'does something' with it. The main part works good BUT the function returns not only what I want but additionally the result of .each loop!
The code:
module Puppet::Parser::Functions
newfunction(:mlh, :type => :rvalue) do |args|
lvm_default_hash = args[0]
lvm_additional_hash = args[1]
if lvm_additional_hash.keys.length == 1
if lvm_additional_hash.keys.include? 'logical_volumes'
# do stuff - we have only 'logical_volumes'
lvm_default_hash.keys.each do |key|
pv_array = Hash['physical_volumes' => lvm_default_hash[key]['physical_volumes']]
lv_hash = lvm_default_hash[key]['logical_volumes']
new_lv_hash = lvm_additional_hash['logical_volumes']
merged_lv_hash = Hash['logical_volumes' => lv_hash.merge(new_lv_hash)]
# this is what I want to return to init.pp
puts Hash[key => pv_array.merge(merged_lv_hash)]
end
end
end
end
end
Variables in the init.pp are:
$default_volume_groups = {
'sys' => {
'physical_volumes' => [
'/dev/sda2',
],
'logical_volumes' => {
'root' => {'size' => '4G'},
'swap' => {'size' => '256M'},
'var' => {'size' => '8G'},
'docker' => {'size' => '16G'},
},
},
}
and the second argument from a hieradata:
modified_volume_groups:
logical_volumes:
cloud_log:
size: '16G'
In the init.pp I have something like this to test it:
notice(mlh($default_volume_groups, $modified_volume_groups))
which gives me a result:
syslogical_volumesvarsize8Gdockersize16Gcloud_logsize16Gswapsize256Mrootsize4Gphysical_volumes/dev/sda2
Notice: Scope(Class[Ops_lvm]): sys
The "long" part before the Notice is the proper result from the puts but the Notice: Scope(): sys is this what I do not want to!
I know that this is the result of this each loop over the default_volumes_groups:
lvm_default_hash.keys.each do |key|
# some stuff
end
How to block of this unwanted result? It blows my puppet's logic because my init.pp sees this sys and not what I want.
Does someone knows how to handle such problem?
Thank you!
I found how to handle this problem but maybe someone could explain me why it works in this way :)
This does not work (short version):
module Puppet::Parser::Functions
newfunction(:mlh, :type => :rvalue) do |args|
lvm_default_hash = args[0]
lvm_additional_hash = args[1]
if lvm_additional_hash.keys.length == 1
if lvm_additional_hash.keys.include? 'logical_volumes'
lvm_default_hash.keys.each do |key|
pv_array = Hash['physical_volumes' => lvm_default_hash[key]['physical_volumes']]
lv_hash = lvm_default_hash[key]['logical_volumes']
new_lv_hash = lvm_additional_hash['logical_volumes']
merged_lv_hash = Hash['logical_volumes' => lv_hash.merge(new_lv_hash)]
puts Hash[key => pv_array.merge(merged_lv_hash)]
end
end
end
end
end
but this works:
module Puppet::Parser::Functions
newfunction(:mlh, :type => :rvalue) do |args|
lvm_default_hash = args[0]
lvm_additional_hash = args[1]
# empty Hash
hash_to_return = {}
if lvm_additional_hash.keys.length == 1
if lvm_additional_hash.keys.include? 'logical_volumes'
lvm_default_hash.keys.each do |key|
pv_array = Hash['physical_volumes' => lvm_default_hash[key]['physical_volumes']]
lv_hash = lvm_default_hash[key]['logical_volumes']
new_lv_hash = lvm_additional_hash['logical_volumes']
merged_lv_hash = Hash['logical_volumes' => lv_hash.merge(new_lv_hash)]
# assigned value in the 'each' loop we want to return to puppet
hash_to_return = Hash[key => pv_array.merge(merged_lv_hash)]
end
# returned Hash - instead of previous 'puts'
return hash_to_return
end
end
end
end
Now I have what I need!
Notice: Scope(Class[Ops_lvm]): sysphysical_volumes/de
You've got it -- the first one doesn't work because in Ruby, the return value of a block or function is the last evaluated statement. In the case of the one that didn't work, the last evaluated statement was the .each. As it turns out, each evaluates to the enumerable that it was looping through.
A simple example:
def foo
[1, 2, 3].each do |n|
puts n
end
end
If I were to run this, the return value of the function would be the array:
> foo
1
2
3
=> [1, 2, 3]
So what you have works, because the last thing evaluated is return hash_to_return. You could even just go hash_to_return and it'd work.
If you wanted to get rid of the return and clean that up a little bit (and if you're using Ruby 1.9 or above), you could replace your each line with:
lvm_default_hash.keys.each_with_object({}) do |key, hash_to_return|
This is because each_with_object evaluates to the "object" (in this case the empty hash passed into the method, and referred to as hash_to_return in the block params). If you do this you can remove the return as well as the initialization hash_to_return = {}.
Hope this helps!
Your custom function has rvalue type which means it needs to return value. If you don't specify return <something> by default, your last statement is implicitly your return.
In the example above, first one that does not work correctly, has last statement inside each block:
puts Hash[key => pv_array.merge(merged_lv_hash)]
Your second example is correct simply because you set value for hash_to_return in each block and then "return" it outside of each block. Not sure if this is the behavior you want since last assigned hash value (in last loop inside each block) will be the one that will be returned from this function.
Related
I'd like to calculate the difference for various values inside 2 hashes with the same structure, as concisely as possible. Here's a simplified example of the data I'd like to compare:
hash1 = {"x" => { "y" => 20 } }
hash2 = {"x" => { "y" => 12 } }
I have a very simple method to get the value I want to compare. In reality, the hash can be nested a lot deeper than these examples, so this is mostly to keep the code readable:
def get_y(data)
data["x"]["y"]
end
I want to create a method that will calculate the difference between the 2 values, and can take a method like get_y as an argument, allowing me to re-use the code for any value in the hash. I'd like to be able to call something like this, and I'm not sure how to write the method get_delta:
get_delta(hash1, hash2, get_y) # => 8
The "Ruby way" would be to pass a block:
def get_delta_by(obj1, obj2)
yield(obj1) - yield(obj2)
end
hash1 = {"x" => { "y" => 20 } }
hash2 = {"x" => { "y" => 12 } }
get_delta_by(hash1, hash2) { |h| h["x"]["y"] }
#=> 8
A method could be passed (indirectly) via:
def get_y(data)
data["x"]["y"]
end
get_delta_by(hash1, hash2, &method(:get_y))
#=> 8
Building on Stefan's response, if you want a more flexible get method you can actually return a lambda from the function and pass arguments for what you want to get. This will let you do error handling nicely:
Starting with the basics from above...
def get_delta_by(obj1, obj2)
yield(obj1) - yield(obj2)
end
hash1 = {"x" => { "y" => 20 } }
hash2 = {"x" => { "y" => 12 } }
get_delta_by(hash1, hash2) { |h| h["x"]["y"] }
Then we can define a get_something function which takes a list of arguments for the path of the element to get:
def get_something(*args)
lambda do |data|
args.each do |arg|
begin
data = data.fetch(arg)
rescue KeyError
raise RuntimeError, "KeyError for #{arg} on path #{args.join(',')}"
end
end
return data
end
end
Finally we call the function using the ampersand to pass the lambda as a block:
lambda_getter = get_something("x","y")
get_delta_by(hash1, hash2, &lambda_getter)
That last bit can be a one liner... but wrote it as two for clarity here.
In Ruby 2.3, you can use Hash#dig method, if it meets your needs.
hash1.dig("x", "y") - hash2.dig("x", "y")
#=> 8
I have a hash of :symbols => values, the values of which will be either a fixnum or a boolean. I have to run a loop that'll call each (&:symbol) to be called on the main argument.
The default hash I'm working with is:
default_options = {
:times => 1,
:upcase => false,
:reverse => false,
}
And the loop I'm using to call them, assuming I've passed an options hash through the method to change one of the defaults and merged them.
hash.each do |key,val|
unless hash[key] == default_options[key]
result = key.to_proc.call(string)
end
end
So basically, if I pass an options hash that changed the defaults, it's supposed to run each proc. If I pass :times => 5, it should print the string 5 times, it should print it upcased if I put :upcase => true, etc in the options hash. So how do you write the proc in symbol form to receive arguments, or make a boolean prove true?
This will kind of do what I think you want, but you shouldn't go messing with String like I did (just to add the times method).
String.instance_exec { alias_method :times, :* }
def do_it(string, **options)
default_options = {
times: 3,
upcase: false,
reverse: false,
}
default_options.merge(options).each do |key, value|
if value
string = value == true ? string.public_send(key) : string.public_send(key, value)
end
end
string
end
Note, I didn't use to_proc here because it's easier to just call the method.
I'm faced with a syntax that I don't quite understand. This is the code:
config.middleware.insert_before 0, "Rack::Cors",:logger => (-> { Rails.logger }) do
allow do
origins '*'
resource '/cors',
:headers => :any,
:methods => [:post],
:credentials => true,
:max_age => 0
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options, :head],
:max_age => 0
end
end
After do in the first line, there is no variable declared as we will do in a regular block, for example:
array.each do |element|
puts element
end
How should I interpret the first example?
It is a block that takes no block variables, or a block to which block variables may be passed but are not used.
Block variables are useful when, while defining the block, you aren't certain what the used values are going to be. However, if you already have access to those values, you can use them directly instead of relying on the variables passed to a block.
Let me provide some very basic examples:
# run the following code
class CB
def self.show_num
yield
end
end
CB.show_num do
1
end
In case you don't know what values will be used, you make the block flexible i.e. have it expect an argument.
# run the following code
class CB
CONSTA = 1
CONSTB = 2
def self.show_with_sign
val = yield(CONSTA, CONSTB).round(2)
"#{val}%"
end
end
CB.show_with_sign do |num, den|
100 * num.fdiv(den)
end
In the code you have shared, all the information is already present where the block is being declared. In simpler words, you already have the values to generate the output for the block. Thus it is handed off to the method as is, without any arguments.
It is not mandatory to pass block variables to the block:
▶ def helloer &cb
▷ puts cb.call
▷ end
# => :helloer
▶ helloer { 'Hello, world' }
Hello, world
# => nil
It's not necessary that a block has variables. Check this out, for example:
array = [1, 2, 3]
array.each do
puts "Hello"
end
# => Hello
# Hello
# Hello
How can I handle a large number of conditions in a case statement?
...I'm about to write a case statement with about 125 when's.
This is along the lines of what I'm doing now, based on each when I add a node to a Nokogiri XML document, each when has two values that get set in the node, before setting the namespace:
case var
when :string
property_uom_node = Nokogiri::XML::Node.new "test_value", #ixml.doc
property_uom_node['att'] = "val"
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{|ns| ns.prefix=="dt"}
property_uom_node
when :integer
#do something else
when :blue
...
#100 more when statements
...
end
I'm not looking for domain specific advice, just if there is a clean way to do this without ending up with a 300 line method.
This is what I ended up doing:
lookup = {:lup => ["1","2"], :wup => ["1","2"]}
case param
when lookup.has_key?(param)
property_uom_node = Nokogiri::XML::Node.new "#{lookup[param][0]}", #ixml.doc
property_uom_node['att'] = #{lookup[param][1]}
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{|ns| ns.prefix=="dt"}
property_uom_node
end
Many case statements can, and many should, be replaced with other structures. Basically, the idea is to separate the policy -- what you want the code to do -- from the implementation -- how the code does it.
Suppose that your case statement is keyed on a symbol (that is, each of then when clauses is a constant symbol):
case foo
when :one
puts 1
when :two
puts 2
when :three
puts 3
else
puts 'more'
end
This can be replaced mostly with a data structure:
INTS = {:one => 1, :two => 2}
key = :one
puts INTS[key] # => 1
What if there are two different values, and not just one? Then make each value its own hash:
DOGS = {
:dog1 => {:name => 'Fido', :color => 'white},
:dog2 => {:name => 'Spot', :color => 'black spots'},
}
key = :dog2
dog = DOGS[key]
puts "#{dog[:name]}'s color is #{dog[:color]}"
# => "Spot's color is black spots"
It looks like the second case statement only has one case. A hash is a good way to do a lookup(many cases). You might try it like this:
if val = lookup[param]
property_uom_node = Nokogiri::XML::Node.new(val[0], #ixml.doc)
property_uom_node['att'] = val[1]
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{ |ns| ns.prefix == "dt" }
property_uom_node # return the node
else
# not one of our cases
end
I get the following error when I try sorting a array with objects inside
undefined method `match_id' for #
I am getting the object back fine without calling sort on it (both sort attempts result in same error)
get '/' do
content_type :json
#matches = []
build_matches_object(#matches, 'C:\Users\Steve\Desktop\BoxRec Boxing Records_files\BoxRec Boxing Records.htm')
#matches.sort! { |a,b| a.match_id <=> b.match_id }
##matches.sort_by { |a| [a.match_id] }
#matches.to_json
end
The object is created in the following function (build_matches_object)
def build_matches_object(myscrape, boxrec_path)
doc = Nokogiri::HTML(open(boxrec_path))
match_date = ''
doc.xpath("//table[#align='center'][not(#id) and not(#class)]/tr").each do |trow|
#Try get the date
if trow.css('.show_left b').length == 1
match_date = trow.css('.show_left b').first.content
match_date = Time.parse(match_date)
end
#if a match row
if trow.css('td a').length == 2 and trow.css('* > td').length > 10
#CODE REMOVED THAT GETS THE BELOW VARIABLES USED TO BUILD MATCH (KNOW IT RETURNS THEM FINE
#create the match object
match = {
:number_of_rounds => trow.css('td:nth-child(3)').first.content.to_i,
:weight_division => trow.css('td:nth-child(4)').first.content,
:first_boxer_name => first_boxer_td.css('a').first.content,
:first_boxer_href => first_boxer_href,
:second_boxer_name => second_boxer_td.css('a').first.content,
:second_boxer_href => second_boxer_href,
:date_of_match => match_date,
:rating => rating,
:match_id => matchid
}
myscrape.push(match)
end
end
end
What is it with the sort that I am doing wrong?
You're assuming it's an object with a match_id method, whereas it appears to be a simple hash.
a[:match_id] <=> b[:match_id]