what does this code do ? from libxml schema-test.rb ???

A

aktxyz

At the bottom of the schema-test.rb in the libxml gem, there is this
bit of code.

This is when I realize I still have a long way to go with Ruby. I
don't get the {} block right after the 1st line, how does this work,
its like a {} block in do-end block ? weird.

Of course, what I am after is the message in the case schema
validation fails !


if doc.validate_schema(schema) { |message, error| puts "#{error ?
'error' : 'warning'} : #{message}" }
puts "validation passed"
else
puts "validation failed"
end
 
R

Reuben Grinberg

At the bottom of the schema-test.rb in the libxml gem, there is this
bit of code.

This is when I realize I still have a long way to go with Ruby. I
don't get the {} block right after the 1st line, how does this work,
its like a {} block in do-end block ? weird.

Of course, what I am after is the message in the case schema
validation fails !


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
 
N

Nobuyoshi Nakada

Hi,

At Fri, 20 Apr 2007 15:13:12 +0900,
Volkan Civelek wrote in [ruby-talk:248525]:
ruby bar.rb gives "No such file to load" error....

What's the exact message?
if you rename foo.so with foo.rb and modify bar.rb like require 'foo' or require 'foo.rb'
it all just fine...

What's that foo.so, an extension library or a ruby script?
 
A

aktxyz

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 herehttp://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-pro...

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

thanks, quite helpful
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,240
Messages
2,571,211
Members
47,845
Latest member
vojosay

Latest Threads

Top