Ruby Runner

Posted on  by 



  • This package will run various script files inside of Atom. It currently supports JavaScript, CoffeeScript, Ruby, Python, Go, Bash and PowerShell scripts. You can add more! Hit Ctrl+R (Alt+R on Win/Linux) to launch the runner for the active window.
  • On Mac, set RUBYCODESIGN environment variable with a signing identity. It uses the identity to sign ruby binary. See also codesign(1). Optionally, run 'make check' to check whether the compiled Ruby interpreter works well. If you see the message 'check succeeded', your Ruby works as it should (hopefully). Run 'make install'.
  • Ruby Muir (born 1 July 1991) is an ultra-endurance runner from New Zealand.In 2017 she won the national women's cross-country event. Muir grew up in Whenaukite on the Coromandel Peninsula in the North Island of New Zealand and attended Mercury Bay Area School. While at high school she became involved with training on Mount Ruapehu for the Hillary Adventure Race, a five-day hiking.
  1. Ruby Runner
  2. Run Ruby Online
  3. Ruby Runner Aquatic Plant
  4. How To Run Ruby Gem
  5. Ruby Runner Script

Run code snippet or code file for multiple languages: C, C++, Java, JavaScript, PHP, Python, Perl, Perl 6, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F# Script, F# (.NET Core), C# Script, C# (.NET Core), VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml Script, R, AppleScript, Elixir, Visual Basic .NET, Clojure, Haxe, Objective-C, Rust, Racket, Scheme, AutoHotkey, AutoIt, Kotlin, Dart, Free Pascal, Haskell, Nim, D, Lisp, Kit, V, SCSS, Sass, CUDA, Less, Fortran, and custom command

Note that the rvm: key is only available in Ruby Build Environments, not in other images containing a ruby implementation. As we upgrade both RVM and Rubies, aliases like 2.2 or jruby point to different exact versions and patch levels.

Sponsors

Increase your coding productivity with Tabnine’s AI code completions! Tabnine is a free powerful Artificial Intelligence assistant designed to help you code faster, reduce mistakes, and discover best coding practices - without ever leaving the comfort of VS Code.

Tabnine is trusted by more than a million developers worldwide. Get it now.

Eliminate context switching and costly distractions. Create and merge PRs and perform code reviews from inside your IDE while using jump-to-definition, your keybindings, and other IDE favorites. Learn more.

Book for VS Code

《Visual Studio Code 权威指南》:带你深入浅出 VS Code!

WeChat Official Account

Ruby Runner

VS Code 的热门文章、使用技巧、插件推荐、插件开发攻略等,请关注“玩转VS Code”公众号!

Donation

If you like this extension, you could become a backer or sponsor via Patreon, donate via PayPal, or scan below QR code to donate via Alipay. Any amount is welcome. It will encourage me to make this extension better and better!

Features

  • Run code file of current active Text Editor
  • Run code file through context menu of file explorer
  • Run selected code snippet in Text Editor
  • Run code per Shebang
  • Run code per filename glob
  • Run custom command
  • Stop code running
  • View output in Output Window
  • Set default language to run
  • Select language to run
  • Support REPL by running code in Integrated Terminal

Usages

  • To run code:
    • use shortcut Ctrl+Alt+N
    • or press F1 and then select/type Run Code,
    • or right click the Text Editor and then click Run Code in editor context menu
    • or click Run Code button in editor title menu
    • or click Run Code button in context menu of file explorer
  • To stop the running code:
    • use shortcut Ctrl+Alt+M
    • or press F1 and then select/type Stop Code Run
    • or right click the Output Channel and then click Stop Code Run in context menu
  • To select language to run, use shortcut Ctrl+Alt+J, or press F1 and then select/type Run By Language, then type or select the language to run: e.g php, javascript, bat, shellscript...
  • To run custom command, then use shortcut Ctrl+Alt+K, or press F1 and then select/type Run Custom Command

Configuration

Make sure the executor PATH of each language is set in the environment variable.You could also add entry into code-runner.executorMap to set the executor PATH.e.g. To set the executor PATH for ruby, php and html:

Supported customized parameters

  • $workspaceRoot: The path of the folder opened in VS Code
  • $dir: The directory of the code file being run
  • $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash
  • $fullFileName: The full name of the code file being run
  • $fileName: The base name of the code file being run, that is the file without the directory
  • $fileNameWithoutExt: The base name of the code file being run without its extension
  • $driveLetter: The drive letter of the code file being run (Windows only)
  • $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)

Please take care of the back slash and the space in file path of the executor

  • Back slash: please use
  • If there ares spaces in file path, please use ' to surround your file path

You could set the executor per filename glob:

Besides, you could set the default language to run:

For the default language: It should be set with language id defined in VS Code. The languages you could set are java, c, cpp, javascript, php, python, perl, ruby, go, lua, groovy, powershell, bat, shellscript, fsharp, csharp, vbscript, typescript, coffeescript, swift, r, clojure, haxe, objective-c, rust, racket, ahk, autoit, kotlin, dart, pascal, haskell, nim, d, lisp

Also, you could set the executor per file extension:

To set the custom command to run:

Ruby Runner

To set the the working directory:

To set whether to clear previous output before each run (default is false):

To set whether to save all files before running (default is false):

To set whether to save the current file before running (default is false):

To set whether to show extra execution message like [Running] ... and [Done] ... (default is true):

[REPL support] To set whether to run code in Integrated Terminal (only support to run whole file in Integrated Terminal, neither untitled file nor code snippet) (default is false):

To set whether to preserve focus on code editor after code run is triggered (default is true, the code editor will keep focus; when it is false, Terminal or Output Channel will take focus):

code-runner.ignoreSelection: Whether to ignore selection to always run entire file. (Default is false)

code-runner.showRunIconInEditorTitleMenu: Whether to show 'Run Code' icon in editor title menu. (Default is true)

code-runner.showRunCommandInEditorContextMenu: Whether to show 'Run Code' command in editor context menu. (Default is true)

code-runner.showRunCommandInExplorerContextMenu: Whether to show 'Run Code' command in explorer context menu. (Default is true)

code-runner.terminalRoot: For Windows system, replaces the Windows style drive letter in the command with a Unix style root when using a custom shell as the terminal, like Bash or Cgywin. Example: Setting this to /mnt/ will replace C:path with /mnt/c/path (Default is ')

code-runner.temporaryFileName: Temporary file name used in running selected code snippet. When it is set as empty, the file name will be random. (Default is 'tempCodeRunnerFile')

code-runner.respectShebang: Whether to respect Shebang to run code. (Default is true)

Runner

About CWD Setting (current working directory)

  1. By default, use the code-runner.cwd setting
  2. If code-runner.cwd is not set and code-runner.fileDirectoryAsCwd is true, use the directory of the file to be executed
  3. If code-runner.cwd is not set and code-runner.fileDirectoryAsCwd is false, use the path of root folder that is open in VS Code
  4. If no folder is open, use the os temp folder

Note

  • For Objective-C, it is only supported on macOS
  • To run C# script, you need to install scriptcs
  • To run TypeScript, you need to install ts-node
  • To run Clojure, you need to install Leiningen and lein-exec

Telemetry data

By default, telemetry data collection is turned on to understand user behavior to improve this extension. To disable it, update the settings.json as below:

Change Log

See Change Log here

Issues

Submit the issues if you find any bug or have any suggestion.

Contribution

Fork the repo and submit pull requests.

  • Ruby Basics
  • Ruby Advanced
  • Ruby Useful Resources
  • Selected Reading

Traditional programs have a single thread of execution the statements or instructions that comprise the program are executed sequentially until the program terminates.

A multithreaded program has more than one thread of execution. Within each thread, statements are executed sequentially, but the threads themselves may be executed in parallel on a multicore CPU, for example. Often on a single CPU machine, multiple threads are not actually executed in parallel, but parallelism is simulated by interleaving the execution of the threads.

Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve concurrency in your code.

Creating Ruby Threads

To start a new thread, just associate a block with a call to Thread.new. A new thread will be created to execute the code in the block, and the original thread will return from Thread.new immediately and resume execution with the next statement −

Example

Here is an example, which shows how we can use multi-threaded Ruby program.

This will produce following result −

Thread Lifecycle

A new threads are created with Thread.new. You can also use the synonyms Thread.start and Thread.fork.

Ruby Runner

There is no need to start a thread after creating it, it begins running automatically when CPU resources become available.

The Thread class defines a number of methods to query and manipulate the thread while it is running. A thread runs the code in the block associated with the call to Thread.new and then it stops running.

The value of the last expression in that block is the value of the thread, and can be obtained by calling the value method of the Thread object. If the thread has run to completion, then the value returns the thread's value right away. Otherwise, the value method blocks and does not return until the thread has completed.

The class method Thread.current returns the Thread object that represents the current thread. This allows threads to manipulate themselves. The class method Thread.main returns the Thread object that represents the main thread. This is the initial thread of execution that began when the Ruby program was started.

You can wait for a particular thread to finish by calling that thread's Thread.join method. The calling thread will block until the given thread is finished.

Threads and Exceptions

If an exception is raised in the main thread, and is not handled anywhere, the Ruby interpreter prints a message and exits. In threads, other than the main thread, unhandled exceptions cause the thread to stop running.

If a thread t exits because of an unhandled exception, and another thread s calls t.join or t.value, then the exception that occurred in t is raised in the thread s.

If Thread.abort_on_exception is false, the default condition, an unhandled exception simply kills the current thread and all the rest continue to run.

If you would like any unhandled exception in any thread to cause the interpreter to exit, set the class method Thread.abort_on_exception to true.

Thread Variables

A thread can normally access any variables that are in scope when the thread is created. Variables local to the block of a thread are local to the thread, and are not shared.

Thread class features a special facility that allows thread-local variables to be created and accessed by name. You simply treat the thread object as if it were a Hash, writing to elements using []= and reading them back using [].

In this example, each thread records the current value of the variable count in a threadlocal variable with the key mycount.

This produces the following result −

The main thread waits for the subthreads to finish and then prints out the value of count captured by each.

Thread Priorities

The first factor that affects the thread scheduling is the thread priority: high-priority threads are scheduled before low-priority threads. More precisely, a thread will only get CPU time if there are no higher-priority threads waiting to run.

You can set and query the priority of a Ruby Thread object with priority = and priority. A newly created thread starts at the same priority as the thread that created it. The main thread starts off at priority 0.

There is no way to set the priority of a thread before it starts running. A thread can, however, raise or lower its own priority as the first action it takes.

Run Ruby Online

Thread Exclusion

If two threads share access to the same data, and at least one of the threads modifies that data, you must take special care to ensure that no thread can ever see the data in an inconsistent state. This is called thread exclusion.

Mutex is a class that implements a simple semaphore lock for mutually exclusive access to some shared resource. That is, only one thread may hold the lock at a given time. Other threads may choose to wait in line for the lock to become available, or may simply choose to get an immediate error indicating that the lock is not available.

By placing all accesses to the shared data under control of a mutex, we ensure consistency and atomic operation. Let's try to examples, first one without mutax and second one with mutax −

Example without Mutax

This will produce the following result −

This will produce the following result −

Handling Deadlock

When we start using Mutex objects for thread exclusion we must be careful to avoid deadlock. Deadlock is the condition that occurs when all threads are waiting to acquire a resource held by another thread. Because all threads are blocked, they cannot release the locks they hold. And because they cannot release the locks, no other thread can acquire those locks.

This is where condition variables come into picture. A condition variable is simply a semaphore that is associated with a resource and is used within the protection of a particular mutex. When you need a resource that's unavailable, you wait on a condition variable. That action releases the lock on the corresponding mutex. When some other thread signals that the resource is available, the original thread comes off the wait and simultaneously regains the lock on the critical region.

Example

This will produce the following result −

Thread States

There are five possible return values corresponding to the five possible states as shown in the following table. The status method returns the state of the thread.

Thread stateReturn value
Runnablerun
SleepingSleeping
Abortingaborting
Terminated normallyfalse
Terminated with exceptionnil

Thread Class Methods

Following methods are provided by Thread class and they are applicable to all the threads available in the program. These methods will be called as using Thread class name as follows −

Ruby Runner Aquatic Plant

How To Run Ruby Gem

Sr.No.Methods & Description
1

Thread.abort_on_exception

Returns the status of the global abort on exception condition. The default is false. When set to true, will cause all threads to abort (the process will exit(0)) if an exception is raised in any thread

2

Thread.abort_on_exception=

When set to true, all threads will abort if an exception is raised. Returns the new state.

3

Thread.critical

Returns the status of the global thread critical condition.

4

Thread.critical=

Sets the status of the global thread critical condition and returns it. When set to true, prohibits scheduling of any existing thread. Does not block new threads from being created and run. Certain thread operations (such as stopping or killing a thread, sleeping in the current thread, and raising an exception) may cause a thread to be scheduled even when in a critical section.

5

Thread.current

Returns the currently executing thread.

6

Thread.exit

Terminates the currently running thread and schedules another thread to be run. If this thread is already marked to be killed, exit returns the Thread. If this is the main thread, or the last thread, exit the process.

7

Thread.fork { block }

Synonym for Thread.new.

8

Thread.kill( aThread )

Causes the given a Thread to exit

9

Thread.list

Returns an array of Thread objects for all threads that are either runnable or stopped. Thread.

10

Thread.main

Returns the main thread for the process.

11

Thread.new( [ arg ]* ) {| args | block }

Creates a new thread to execute the instructions given in block, and begins running it. Any arguments passed to Thread.new are passed into the block.

12

Thread.pass

Invokes the thread scheduler to pass execution to another thread.

13

Thread.start( [ args ]* ) {| args | block }

Basically the same as Thread.new. However, if class Thread is subclassed, then calling start in that subclass will not invoke the subclass's initialize method.

14

Thread.stop

Stops execution of the current thread, putting it into a sleep state, and schedules execution of another thread. Resets the critical condition to false.

Ruby Runner Script

Thread Instance Methods

These methods are applicable to an instance of a thread. These methods will be called as using an instance of a Thread as follows −

Sr.No.Methods & Description
1

thr[ aSymbol ]

Attribute Reference - Returns the value of a thread-local variable, using either a symbol or an aSymbol name. If the specified variable does not exist, returns nil.

2

thr[ aSymbol ] =

Attribute Assignment - Sets or creates the value of a thread-local variable, using either a symbol or a string.

3

thr.abort_on_exception

Returns the status of the abort on exception condition for thr. The default is false.

4

thr.abort_on_exception=

When set to true, causes all threads (including the main program) to abort if an exception is raised in thr. The process will effectively exit(0).

5

thr.alive?

Returns true if thr is running or sleeping.

6

thr.exit

Terminates thr and schedules another thread to be run. If this thread is already marked to be killed, exit returns the Thread. If this is the main thread, or the last thread, exits the process.

7

thr.join

The calling thread will suspend execution and run thr. Does not return until thr exits. Any threads not joined will be killed when the main program exits.

8

thr.key?

Returns true if the given string (or symbol) exists as a thread-local variable.

9

thr.kill

Synonym for Thread.exit.

10

thr.priority

Returns the priority of thr. Default is zero; higher-priority threads will run before lower priority threads.

11

thr.priority=

Sets the priority of thr to an Integer. Higher-priority threads will run before lower priority threads.

12

thr.raise( anException )

Raises an exception from thr. The caller does not have to be thr.

13

thr.run

Wakes up thr, making it eligible for scheduling. If not in a critical section, then invokes the scheduler.

14

thr.safe_level

Returns the safe level in effect for thr.

15

thr.status

Returns the status of thr: sleep if thr is sleeping or waiting on I/O, run if thr is executing, false if thr terminated normally, and nil if thr terminated with an exception.

16

thr.stop?

Returns true if thr is dead or sleeping.

17

thr.value

Waits for thr to complete via Thread.join and returns its value.

18

thr.wakeup

Marks thr as eligible for scheduling, it may still remain blocked on I/O, however.





Coments are closed