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

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

[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

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

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

-v

James B. [email protected] 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

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

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?

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

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

On Apr 20, 12:25 am, Reuben G. [email protected]
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

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.

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 ?

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. [email protected] 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?

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

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. [email protected] 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://groups.yahoo.com/groups/id-ruby
Let’s build Ruby + Rails Indonesia stronger
http://linkedin.com/in/ariekeren

-voc

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

[email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs