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

**URL:** <https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515>\
**Category:** Ruby\
**Created:** [April 20, 2007, 5:50am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515 "2007-04-20T05:50:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 5:50am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/1 "2007-04-20T05:50:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 7:40am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/2 "2007-04-20T07:40:41Z")

</div>

removed\_email\_address@domain.invalid 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](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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 8:13am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/3 "2007-04-20T08:13:43Z")

</div>

hi,  
i know it sounds so trivial but couldn’t tackle…  
here it is…

let’s say you have foo.so file.  
at the same folder you have bar.rb file and on that bar.rb file you  
require ‘foo.so’

ruby bar.rb gives “No such file to load” error…

if you rename foo.so with foo.rb and modify bar.rb like require ‘foo’ or  
require ‘foo.rb’  
it all just fine…

any suggestions are greatly appreciated…

thanks,  
volkan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 8:39am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/4 "2007-04-20T08:39:47Z")

</div>

No. It doesn’t work.  
give it shot…  
i gues it’s a bug then…

-v

James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Volkan Civelek wrote:

> if you rename foo.so with foo.rb and modify bar.rb like require ‘foo’ or require ‘foo.rb’  
> it all just fine…
> 
> any suggestions are greatly appreciated…

[http://www.ruby-doc.org/core/classes/Kernel.html#M005945](http://www.ruby-doc.org/core/classes/Kernel.html#M005945)

Leave off the file extension. Ruby has its own logic for working that  
out with ‘require’.

require ‘foo’

–  
James B.

“I must create a system, or be enslaved by another man’s;  
I will not reason and compare; my business is to create.”

- William Blake

-voc

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 9:18am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/5 "2007-04-20T09:18:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 8:25am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/6 "2007-04-20T08:25:44Z")

</div>

Volkan Civelek wrote:

> if you rename foo.so with foo.rb and modify bar.rb like require ‘foo’ or require ‘foo.rb’  
> it all just fine…
> 
> any suggestions are greatly appreciated…

[http://www.ruby-doc.org/core/classes/Kernel.html#M005945](http://www.ruby-doc.org/core/classes/Kernel.html#M005945)

Leave off the file extension. Ruby has its own logic for working that  
out with ‘require’.

require ‘foo’

–  
James B.

“I must create a system, or be enslaved by another man’s;  
I will not reason and compare; my business is to create.”

- William Blake

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/7 "2007-04-20T16:01:17Z")

</div>

On Apr 20, 12:25 am, Reuben G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In ruby, functions can take as an argument another function to call.  
> puts yield
> 
> ```
> doc.validate_schema(schema) { |message, error| return message }
> 
> ```
> 
> else  
> irb\>puts “a”  
> a  
> irb\>puts “#{a}”  
> 5
> 
> irb\> greeting = “hello”  
> irb\> puts “#{greeting}, Bob!”  
> hello Bob!
> 
> Hope that helps,  
> Reuben

thanks, quite helpful

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 11:28am UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/8 "2007-04-20T11:28:10Z")

</div>

I think that the block is called only if there is an error or a warning  
during the validation.  
Otherwise the block is not called. In case of an error/warning, the  
block  
get the message  
and whether it is an error or a warning. It is possible that the block  
is  
called for each warnings  
and errors. I will check the extension.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/9 "2007-04-20T17:36:27Z")

</div>

Volkan Civelek wrote:

> if you rename app.so to app.rb and change the require line accordingly, it works fine.

yes, btw why did you name it “app.so” when you knew it’s a ruby script ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/10 "2007-04-20T16:59:24Z")

</div>

for the demo purpose, i made it dirt simple…

$ cat app.so  
p ‘v’  
$ cat foo.rb  
require ‘app.so’  
p ‘c’  
$ ruby foo.rb  
foo.rb:1:in `require’: No such file to load – app.so (LoadError)  
from foo.rb:1

if you rename app.so to app.rb and change the require line accordingly,  
it works fine.

-v  
Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: 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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/11 "2007-04-20T18:19:29Z")

</div>

On Sat, 21 Apr 2007, Volkan Civelek wrote:

> well it doesn’t have to be so…  
> any ext other than rb repreduces the problem…  
> krb, xyz, etc…  
> but that besides the point, right?

not really

## harp:~ \> ri Kernel#require --------------------------------------------------------- Kernel#require require(string) =\> true or false

```
  Ruby tries to load the library named _string_, returning +true+ if
  successful. If the filename does not resolve to an absolute path,
  it will be searched for in the directories listed in +$:+. If the
  file has the extension ``.rb'', it is loaded as a source file; if
  the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
  default shared library extension is on the current platform, Ruby
  loads the shared library as a Ruby extension. Otherwise, Ruby 

```

tries  
adding `.rb'',`.so’’, and so on to the name. The name of the  
loaded feature is added to the array in +$"+. A feature will not  
be  
loaded if it’s name already appears in +$"+. However, the file  
name  
is not converted to an absolute path, so that ``+require  
‘a’;require ‘./a’+’’ will load +a.rb+ twice.

```
     require "my-library.rb"
     require "db-driver"

```

you are seeing require behave exactly as it’s doccumented to behave.  
perhaps  
you are after the functionality of ‘load’??

## harp:~ \> ri Kernel#load ------------------------------------------------------------ Kernel#load load(filename, wrap=false) =\> true

```
  Loads and executes the Ruby program in the file _filename_. If the
  filename does not resolve to an absolute path, the file is 

```

searched  
for in the library directories listed in +$:+. If the optional  
_wrap_ parameter is +true+, the loaded script will be executed  
under an anonymous module, protecting the calling program’s global  
namespace. In no circumstance will any local variables in the  
loaded file be propagated to the loading environment.

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/12 "2007-04-20T17:56:28Z")

</div>

well it doesn’t have to be so…  
any ext other than rb repreduces the problem…  
krb, xyz, etc…  
but that besides the point, right?

Arie Kusuma A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Volkan Civelek  
wrote:

> if you rename app.so to app.rb and change the require line accordingly, it works fine.

yes, btw why did you name it “app.so” when you knew it’s a ruby script ?

> > 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?

–  
Arie Kusuma A. A.K.A Arie A.K.A ariekeren / YM! = riyari3  
[http://ariekusumaatmaja.wordpress.com](http://ariekusumaatmaja.wordpress.com)

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com/)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

Let’s build Ruby + Rails Indonesia stronger  
[http://linkedin.com/in/ariekeren](http://linkedin.com/in/ariekeren)

-voc

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 20, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do-from-libxml-schema-test-rb/92515/13 "2007-04-20T19:51:39Z")

</div>

thanks, load works, i was curious about the require…  
so if i require something like require ‘defaults.txt’  
i think ruby looks for “defaults.txt.rb” or “defaults.txt.so” or  
“defaults.txt.whatever the shared library extensions are…”  
anyway, i find this confusing… i am open to suggestions…

-v

removed\_email\_address@domain.invalid wrote: On Sat, 21 Apr 2007, Volkan Civelek wrote:

> well it doesn’t have to be so…  
> any ext other than rb repreduces the problem…  
> krb, xyz, etc…  
> but that besides the point, right?

not really

## harp:~ \> ri Kernel#require --------------------------------------------------------- Kernel#require require(string) =\> true or false

```
  Ruby tries to load the library named _string_, returning +true+ if
  successful. If the filename does not resolve to an absolute path,
  it will be searched for in the directories listed in +$:+. If the
  file has the extension ``.rb'', it is loaded as a source file; if
  the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
  default shared library extension is on the current platform, Ruby
  loads the shared library as a Ruby extension. Otherwise, Ruby 

```

tries  
adding `.rb'',`.so’’, and so on to the name. The name of the  
loaded feature is added to the array in +$"+. A feature will not  
be  
loaded if it’s name already appears in +$"+. However, the file  
name  
is not converted to an absolute path, so that ``+require  
‘a’;require ‘./a’+’’ will load +a.rb+ twice.

```
     require "my-library.rb"
     require "db-driver"

```

you are seeing require behave exactly as it’s doccumented to behave.  
perhaps  
you are after the functionality of ‘load’??

## harp:~ \> ri Kernel#load ------------------------------------------------------------ Kernel#load load(filename, wrap=false) =\> true

```
  Loads and executes the Ruby program in the file _filename_. If the
  filename does not resolve to an absolute path, the file is 

```

searched  
for in the library directories listed in +$:+. If the optional  
_wrap_ parameter is +true+, the loaded script will be executed  
under an anonymous module, protecting the calling program’s global  
namespace. In no circumstance will any local variables in the  
loaded file be propagated to the loading environment.

## -a

be kind whenever possible… it is always possible.

- the dalai lama

-voc
