[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

uncle

4/20/2007 3:45:00 AM

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

3 Answers

Reuben Grinberg

4/20/2007 5:25:00 AM

0

aktxyz@gmail.com wrote:
> 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-a...

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

Nobuyoshi Nakada

4/20/2007 7:18:00 AM

0

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?

--
Nobu Nakada

uncle

4/20/2007 1:57:00 PM

0

On Apr 20, 12:25 am, Reuben Grinberg <reuben.grinb...@aya.yale.edu>
wrote:
> akt...@gmail.com wrote:
> > 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 herehttp://eli.thegreenplace.net/2006/04/18/understanding-ruby-......
>
> 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