Ruby 的 Python 接口 puby

码农软件 · 软件分类 · 其他开发相关 · 2019-10-23 14:57:30

软件介绍

puby 是 Ruby 解释器的 Python 接口。

示例代码

解析 HTML

# python                                   # # ruby
from puby import Object as rb              # 
rb.require("nokogiri")                     # require "nokogiri"
rb.require("open-uri")                     # require "open-uri"

url = "http://www.google.com/search?q=foo" # url = "http://www.google.com/search?q=foo"
doc = rb.Nokogiri.HTML.parse(rb.open(url)) # doc = Nokogiri::HTML.parse(open(url))

for link in doc.css("h3.r a"):             # doc.css("h3.r a").each do |link|
    print link.content()                   #     puts link.content
                                           # end
执行 JavaScript:
from puby import Object as rb              # 
rb.require('v8')                           # require "v8"

ctx = rb.V8.Context.new()                  # ctx = V8::Context.new
print ctx.eval("'Xzibit ' + 7 * 6")        # puts ctx.eval "'Xzibit ' + 7 * 6"
运行 Web 服务器:
from puby import Object as rb              # 
rb.require("rack")                         # require "rack"
                                           # 
def application(env):                      # def application(env)
    return [200,                           #   [200,
            {"Content-Type": "text/html"}, #    {"Content-Type" => "text/html"},
            ["Hello Puby!"]]               #    ["Hello Puby!"]]
                                           # end
rb.Rack.Handler.Thin.run(application)      # Rack::Handler::Thin.run method(:application)
回调示例:
from puby import Object as rb, RbCallback

rb.eval("""
class TestObj
    def save_callback cb
        @cb = cb
    end
    def call_callback x
        @cb.call(x)
    end
end
""")
obj = rb.TestObj.new()

def callback(x):
    return x+1

callback_rb = RbCallback(callback)
obj.save_callback(callback_rb)
print obj.call_callback(5)

本文地址:https://codercto.com/soft/d/17392.html

Fortran 95/2003程序设计

Fortran 95/2003程序设计

中国电力出版社 / 2009-8 / 88.00元

Fortran是计算世界最早出现的高级程序设计语言之一,随着面向对象编程时代的到来,Fortran语言不仅保持了发展的步伐,而且继续在科学计算方面领先。《Fortran95/2003程序设计(第3版)》在第2~7章介绍了Fortan语言基础知识,为初学者提供入门学习资料;在第8~15章介绍了Fortran语言高级特性,为深入用好Fortran语言提供支持;在第16章讲述了Fortran语言面向对象......一起来看看 《Fortran 95/2003程序设计》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码