Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Ruby"

From NetSec
Jump to: navigation, search
Line 4: Line 4:
 
=Development Environment=
 
=Development Environment=
 
==ruby cli==
 
==ruby cli==
 +
Type irb at the commandline to use the default interpreter. If you have rubinius, an interpreter optimized for performance similar to how the sbcl or pypy interpreters are, its rbx. If you want to have autocompletion and a nicer interface, installing the pry interpreter gem through your package manager or through rubygems is a good idea.
 
==irb==
 
==irb==
 
==gem==
 
==gem==
Line 21: Line 22:
 
  #!/usr/bin/ruby
 
  #!/usr/bin/ruby
  
  def asciispeek(x)
+
def asciispeek(x)
  x.each_byte do |c|
+
      x.each_byte do |c|
 
     puts c.to_s(2)
 
     puts c.to_s(2)
 
end
 
end

Revision as of 02:17, 24 June 2012

Ruby is one of many interpreted languages written in C used in Linux systems for command line tools and serving web applications. Ruby on Rails is an object-oriented MVC framework written in ruby served by WEBRICK and mongrel on Linux systems.

RPU0j.png This article needs immediate attention, and is in desperate need of content.

Development Environment

ruby cli

Type irb at the commandline to use the default interpreter. If you have rubinius, an interpreter optimized for performance similar to how the sbcl or pypy interpreters are, its rbx. If you want to have autocompletion and a nicer interface, installing the pry interpreter gem through your package manager or through rubygems is a good idea.

irb

gem

Your first application

Variables and data types

Boolean Logic

Loops

User Input

User-Defined Functions

Security

Examples of Useful Programs

Many times a hacker has stopped to ask how a character is represented in ascii, possibly for alphanumeric shellcode. Rather than having to resort to wikipedia, why not throw together a script? We'll call it asciispeek.rb

 
 
 #!/usr/bin/ruby
 
def asciispeek(x)
       x.each_byte do |c|
    		puts c.to_s(2)
	end
end
 
asciispeek(ARGV.join(' ').to_s)
 
 

This script takes the arguments from the shell (ARGV) and joins them into one string. The act of joining removes the whitespace between them, so we add it back in, and then convert it to a string. The result is then passed to our function. Our function calls an enumerable method of the string class, each_byte, which gets at the byte representation of the character. We then pipe it into a block which converts it into a base 2 number and then prints it. A simple edit could make this spit out the hex representation, or the octal. This is a good example of the idea of reusing code. By encapsulating the actual actions into a function, we can copy this function into a class or script that we make in the future.

Ruby is part of a series on programming.
<center>
</center>