从ruby脚本逐字运行命令行字符串

时间:2015-08-14 17:39:43

标签: ruby command-line terminal command command-line-interface

我可以通过// // playSoundsViewController.swift // recordVoice // // Created by david on 11/08/15. // Copyright (c) 2015 Tomcorp. All rights reserved. // import UIKit import AVFoundation class playSoundsViewController: UIViewController { var audioPlayer:AVAudioPlayer! var receivedAudio:RecordedAudio! override func viewDidLoad() { super.viewDidLoad() audioPlayer = AVAudioPlayer(contentsOfURL: receivedAudio.filePathUrl, error: nil) audioPlayer.enableRate = true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func slowVoice(sender: UIButton) { audioPlayer.stop() audioPlayer.rate = 0.5 audioPlayer.currentTime = 0.0 audioPlayer.play() } @IBAction func fastVoice(sender: UIButton) { audioPlayer.stop() audioPlayer.rate = 2.0 audioPlayer.currentTime = 0.0 audioPlayer.play() } @IBAction func stopButtonSounds(sender: UIButton) { audioPlayer.stop() } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ // // recordSoundsViewController.swift // recordVoice // // Created by david on 11/08/15. // Copyright (c) 2015 Tomcorp. All rights reserved. // import UIKit import AVFoundation class recordSoundsViewController: UIViewController, AVAudioRecorderDelegate { @IBOutlet weak var recordButton: UIButton! @IBOutlet weak var recordingLabel: UILabel! @IBOutlet weak var stopButton: UIButton! var audioRecorder:AVAudioRecorder! var recordedAudio:RecordedAudio! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillAppear(animated: Bool) { stopButton.hidden = true recordButton.enabled = true } @IBAction func recordButton(sender: UIButton) { recordButton.enabled = false stopButton.hidden = false recordingLabel.hidden = false //record voice //Inside func recordAudio(sender: UIButton) let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String let recordingName = "my_audio.wav" let pathArray = [dirPath, recordingName] let filePath = NSURL.fileURLWithPathComponents(pathArray) println(filePath) var session = AVAudioSession.sharedInstance() session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil) audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil) audioRecorder.delegate = self audioRecorder.meteringEnabled = true audioRecorder.record() } func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) { if(flag){ recordedAudio = RecordedAudio() recordedAudio.filePathUrl = recorder.url recordedAudio.title = recorder.url.lastPathComponent self.performSegueWithIdentifier("stopRecording", sender: recordedAudio) }else { println("Recording was not successfully") recordButton.enabled = true stopButton.hidden = true } } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if (segue.identifier == "StopRecording"){ let playSoundsVC:playSoundsViewController = segue.destinationViewController as! playSoundsViewController let data = sender as! RecordedAudio playSoundsVC.receivedAudio = data } } @IBAction func stopButton(sender: UIButton) { recordButton.enabled = true recordingLabel.hidden = true //Inside func stopAudio(sender: UIButton) audioRecorder.stop() var audioSession = AVAudioSession.sharedInstance() audioSession.setActive(false, error: nil) } } // // RecordedAudio.swift // recordVoice // // Created by david on 13/08/15. // Copyright (c) 2015 Tomcorp. All rights reserved. // import Foundation class RecordedAudio: NSObject{ var filePathUrl: NSURL! var title: String! } // // AppDelegate.swift // recordVoice // // Created by david on 11/08/15. // Copyright (c) 2015 Tomcorp. All rights reserved. // import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }

命令行运行ruby脚本

在该文件中,我可以执行`puts“this_and_that_string”

但是如何将我放在一起的字符串表现得好像我直接在命令行中键入它们来执行命令?

e.g。 ruby脚本中的ruby 'name of file'表现得就像我输入命令行一样: some_function cd \/var\/

1 个答案:

答案 0 :(得分:1)

简短的回答是在ruby代码中使用反引号来命令命令行。对此的一个很好的解释发布在这里:Calling shell commands from Ruby