【转】ruby 调用其他函数

1、获取其他程序的运行结果

x= system("date")

x='date'

x=%x{date}



2、调用其他程序,同时把执行权移交给被调程序

exec "shutdown -s -t 0"

puts "this will never be displayed!"



3、同时运行两个程序

forking就是值程序的实例进程复制自身,导致该程序的两个进程并发运行。


if fork.nil?<pre class="ruby" name="code">if fork.nil?
exec "ruby some_other_file.rb"
end

puts "this ruby script now run!"


4、与另一个程序进行交互

5、安全级别$SAFE,在drb中有使用过该特性,防止客户端调用server服务执行非法操作

6、使用window的API
[code = "ruby"]
require "Win32API"

title = "My Application"
text = "Hello world"
Win32API.new('user32','MessageBox' , %w{L P P L},'I').call(0,text,title,0)
[/code]
7、控制windows程序

对windows的自动化访问是通过ruby的WIN32OLE程序库实现的,例如

[code]
require "Win32API"
require "win32ole"

title = "My Application"
text = "Hello world"
result = Win32API.new('user32','MessageBox' , %w{L P P L},'I').call(0,text,title,1)

case result
when 1
puts "clicked OK"
when 2
puts "clicked cancel!"
else
puts "clicked something else!"
end

web_browser = WIN32OLE.new('InternetExplorer.Application')
web_browser.visible = true
web_browser.navigate('http://www.baidu.com')
[/code]

转自: [url]http://blog.csdn.net/zhanggs007/article/details/7606525[/url]
在C++中调用Ruby代码 #include static int id_sum;int Values[] = {5, 10 ,15,-1,20,0};static VALUE wrap_sum(VALUE args){ VALUE * values = (VALUE *) args; VALUE summer = values[0]; VALUE max = values[1]; retur 阅读详情

相关推荐

Ruby报错“SystemStackError“?尾调用优化的Ruby实现方案

摘要:本文探讨Ruby中常见的"SystemStackError"递归栈溢出问题及解决方案。通过分析递归导致栈爆炸的机制,提出三种优化方案:1)尾调用优化(TCO)改写递归为循环;2)使用Recur模式模拟TCO;3)替代的非递归实现方法。性能对比显示,迭代法处理10,000元素时执行时间从递归崩溃优化至15ms,内存占用降至2.1MB。建议开发者根据场景复杂度选择合适方案,优先使用内置方法和迭代法处理深度递归问题,必要时通过TCO保留递归的简洁性。

shejizuopin的博客 1025

12 种方式轻松实现 Ruby 调用

作者 |Gregory Witek译者 | 弯月,责编 | 王晓曼头图 | CSDN 下载自东方IC出品 | CSDN(ID:CSDNnews)以下为译文:最近,与同事聊天的时候,我们...

AI科技大本营 7003

Ruby-PyCall:在Ruby调用Python函数的实战指南

本文还有配套的精品资源,点击获取 简介:Ruby-PyCall是一个允许Ruby代码调用Python函数和模块的库,它通过无缝集成增强了Ruby的生态系统。使用PyCall可以轻松在Ruby中利用Python的科学计算和数据处理功能,提高代码复用性并扩展项目功能。本文提供了PyCall的安装指南,以及如何在Ruby调用Python函数的示例和注意事项,包括类型换和性能考...

weixin_42471823的博客 1423

Ruby中的各种方法定义和调用函数式方法|实例方法|类方法)

1.ruby方法的定义 (1)不带参数的方法 def function_name   statement …… end 调用方式: function_name or function_name() (2)带参数的方法定义 def function_name(parm1, parm2, …)   statement …… end 调用方式: function_

@_囚徒-2018_的家园 1万+

ruby 方法之间的调用

def hi name puts "hi" + name end def hi_back v v.call(:hi) end hi_back method(:hi) #=> hi aa ############################## def hi name "hi aa" end def hi_back yield end hi_back

zhihuichina的博客 1537

ruby函数调用

#! /usr/bin/ruby # encoding:gbk nums = Array.new(10) { |e| e = e * 2 } def test(paraList)     $i = 0     $num = 30     for i in 0...$num     if i == 2 || i == 3     next     end    

随手记 944

Ruby语言基础学习六:Ruby模块、引用其他程序、Mixins

新建一个trig.rb程序并写入: #-*-coding:UTF-8 -*- #Ruby模块Module # 模块提供了一个命名空间和避免名字冲突。模块实现了 mixin 装置。 module Trig PI=3.14 def Trig.sin(x) puts "sinx" end def Trig.cos(x) puts "cosx" end end 再新建一个mo

风哥有点田 1535

ruby调用python_PyCall: 从Ruby语言调用Python函数

PyCall: Calling Python functions from the Ruby language This library provides the features to directly call and partially interoperate with Python from the Ruby language. You can import arbitrary Py...

weixin_39566578的博客 476

C代码中调用Ruby

最近的项目使用了,从Ruby调用C的代码还是比较简单,尤其是利用SWIG来生成代码。 现在有个问题,怎样从C/C++中调用ruby的代码? 查看了网上一些资料,发现这方便的东西太少。找到http://blog.csdn.net/daiyuchao/archive/2008/07/08/2625775.aspx,不过这个编译之后,运行会出错。后来查看了:http://blad

wu4long的专栏 4488

ruby 构造函数_Ruby构造函数

ruby 构造函数 Ruby中的构造函数 (Constructors in Ruby) Constructors are one of the most important parts of Object-Oriented programming. A constructor of a class is a unique method which is invoked or called when...

cumtb2009的博客 654

Ruby Controller中方法的调用

在controller中可以通过params[:字段]的方式来获取发送过来的数据, 无论他们是来自get中的url中?后面,还是来自post数据,rails不区分这两种参数 context "#test" do con = AccountsController.new before do @params = {mobile_number: '128888888888'} allow(JrOpenClient::Api::Login).to receive(:send_sms).and

AtlanticYu的博客 632

一个文件中调用一个文件的函数

  ceshi_1.c源码如下所示: #include&lt;stdio.h&gt; #include&lt;sys/types.h&gt; #include&lt;stdlib.h&gt; int f(int n); int main() {int n; printf("I LOVE YOU"); printf("\n"); printf("%d",f(3));..

weixin_34319111的博客 818

ruby使用终端启用外部程序

可以使用两个、一个是exec、一个是system、两个都可以执行命令行、但用前者的话执行到exec这行后会自动终止整个程序、所以迩还想 返回到原程序继续执行以下的代码可以使用system   system("ruby -v")...

weixin_34401479的博客 226

Ruby函数调用与super

最近在巩固Ruby 的基本语法知识,在这里把以前一些不知道的特性一一总结下来。 在Ruby中是允许method的重复声明的,并且在合适的时候还允许你去一个一个调用(利用super关键字)。 在这里通过几个实例,我将和大家分享一下Ruby的method查找路径和方式以及super的使用和参数问题。 首先,让大家来看看这样一个例子: class A def a p 'a 1' end ...

PC的€$~!@#% 307

ruby 调用winapi

获取鼠标require Win32API getCursorPos = Win32API.new("user32", "GetCursorPos", [P], V) lpPoint = " " * 8 getCursorPos.Call(lpPoint) x, y = lpPoint.unpack("LL") print "x: ", x, "/n" print "y: ", y,

在水一方 1300

ruby在类中访问@,类外访问调用方法

class Box def initialize(w,h) @width,@height=w,h end def printWidth puts @width end def printHeight @height*12 endputs @width,"asfd" 这访问不了,只能在实例方法中访问,这里代表类实例class ...

weixin_34130389的博客 329

Ruby中通过require引用文件

自: http://www.51testing.com/html/79/605679-851921.html 一、引用单个文件: test目录下有两个文件file1.rb,file2.rb 在file1.rb中: require 'file2'会报错,可以使用以下方法引用file2: 1、require File.dirname(__FILE__)+'/file2'   

LUCIEN06的专栏 2589
上一篇: IT 牛人汇总
下一篇: 【转】两年外包女程序员的真实感受
hwlzs
博客等级 码龄15年 11粉丝 258原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值