[email protected] wrote:
if doc.validate_schema(schema) { |message, error| puts “#{error ?
‘error’ : ‘warning’} : #{message}” }
puts “validation passed”
else
puts “validation failed”
end
I’m not sure exactly which set of {}'s you are referring to, so I’ll
explain all of them.
In ruby, functions can take as an argument another function to call.
(More detailed information here
http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods
but I’ll summarize)
Here is an example of a function that takes another function as an
argument, and then calls that function:
irb>def print_it
#yield is a special keyword that calls the passed function
puts yield
end
irb> print_it { “bob” }
bob
Furthermore, the function passed in can take an argument:
irb>def print_it2
puts yield(3)
end
irb> print_it { |x| x + 1 }
4
So in your code,
if doc.validate_schema(schema) { |message, error|
message is the message from validating and error is the error…
So if you wanted to make a function that returned the message:
def validate(doc, schema)
doc.validate_schema(schema) { |message, error| return message }
end
Or, if you want to get the error and message:
the_message, the_error = nil
if doc.validate_schema(schema) { |message, error|
the_message, the_error = message, error
puts “#{error ? error’ : ‘warning’} : #{message}”
}
puts “validation passed”
else
puts “validation failed”
end
Are you asking about #{message}?
This is the way to get the value of a variable inside a string:
irb>a = 5
irb>puts a
5
irb>puts “a”
a
irb>puts “#{a}”
5
irb> greeting = “hello”
irb> puts “#{greeting}, Bob!”
hello Bob!
Hope that helps,
Reuben