Getting the Windows username using Ruby

Tags:

It’s really quite amazing the power of Ruby! I’ve been learning Ruby for a little over 5 weeks and already I know way too much than I should. Well, have yet to learn Active Directory but I’m getting there. Below is an example of a program I developed to show how to get the windows username. I added an option to the Ruby script to create a network drive by using the DOS command, “net use”.

Well, here’s the script, feel free to use or redistribute with my details intact.

require 'win32ole'
network=WIN32OLE.new("Wscript.Network")

currentUser = ENV['username']

require 'Win32API'
name = " " * 128
size = "128"
Win32API.new('advapi32','GetUserName',['P','P'],'I').call(name,size)

stringDrive = "\\\\PRISERVER\\mtn_shares\\hdrivespri\\certificateiv\\#{currentUser}"

puts "#{stringDrive}"

system "net use o: #{stringDrive}"

system "PAUSE"

Calcuating the Radius of a Circle – Ruby

In order to calculate the area of a circle you need to know how to calculate radius square. Well in Ruby you would think it would be a, “^” character. However it’s is “**” isntead. So, 2**2 would be 4. Below is an example program which will calculate the area of a circle as well as it’s circumference.


# !/user/bin/env ruby
# Programmer: Nicholas O'Sullivan, E - <a href="mailto:nick@autechnet.com">nick@autechnet.com</a>
# Cert IV Information Technology
#
# Date Modified - 12 August 2010
# Version: 1.0
# Name: Circumference of a Circle
# Purpose: This program will be calculating the circumference of a circle. Using the area = Pi * (radius **2) Circumference = 2 * Pi * radius
# 

loop do

# Declare what value pi will be.
pi = 3.141592

system "cls" # Clear for easy reading.

puts "This program will be calculating the Circumference of a Circle. \nThe value we are using for pi is 3.141592.\nPlease enter in the Radius?\n"
radius = gets.chomp.to_f # Store in float as circumfernce is usually in decimals thanks to pi being 3.14159265358979323846264338327950288419716939937510

area = pi * (radius**2)
area = "%.4f" % area # Round it up to 4 decimal places for accuracy
puts "The Area of the circle is - #{area}"
circ = 2 * radius * pi
circ = "%.4f" % circ # Round it up to 4 decimal places for accuracy
puts "The Circumference of the circle is - #{circ}"
  
system "Pause" # Pause for the use to check the answer.  

end

Planning a Program

When developing a program it is crucial that you have adequate planning. This is usually accomplished through writing a simple plan consisting of; IPO Chart, Test Data, Variables, Expected Result, Pseduo code and the code related to the end program.

Why complete a plan? Well completing a plan enables you to come back to all that the project your working on is aiming to do. It enables you to stay focused on your task for the project. Usually programmers work in teams which means everyone gets given a small section of the program to develop and work on. At the end of all the program when the final program is completed each programmer needs to know what others are programming. It’s also a safety net if you were ever to quit your job there is record of the program you were working on.

Below is a plan template that I use in programming – Ruby Example Plan.

IPO

The IPO section in planning a program outlays the Input, Processing and Output. Hence the IPO. Fairly simple usually says something like this:

INPUT

prompt user for variable1
prompt user for variable
prompt user for variable3

PROCESSING

calculate the answer by using the variable – “answer”

OUTPUT

Display on the screen the Number Sentence with answer after the proceeding “=” sign.

Test Data

This section is just a little bit of data that you can use to test the program out. Otherwise who knows you could be calculating false results and calling operations to run on a computer that shouldn’t have to run. Below is an example.

TEST DATA

Variable1 Variable2                            Variable3

1                                              2                                              3
5                                              6                                              1
89                                           12                                           3

EXPECTED RESULT

1 + 2 – 3 = 0
5 + 6 – 1 = 10
89 + 12 – 3 = 98

Pseduo Code

Pseduo code is not the final code from the program. Pseduo is pronounced, “Sudo-code.” It is essentially what you would like the program to do in English without all the needed grammatical characters.

get variable1
get variable2
get variable2

CALCULATE answer(float) AS variable1 + variable2 – variable3

Ruby Code

This section should be titled the type code that you are programming in. For instance if your programming in C++ then title this section C++ Code.


#!/user/bin/env ruby
# Programmer: Nicholas O'Sullivan, E - <a href="mailto:nick@autechnet.com">nick@autechnet.com</a>
# Cert IV Information Technology
#
# Date Modified - 19 July 2010
# Version: 2.0
# Program Name: Ruby Practice 1
# Purpose:          Calculating 3 values that a user inputs and outputting in a
#                             number sentence the operation performed and answer
#

puts "Plese enter the 3 numbers below as variables.\n"
puts "Variable 1: "
value_one = gets.chomp.to_f # can be f for float, i for integer, d for double
puts "\n"
puts "Variable 2: "
value_two = gets.chomp.to_f
puts "\nVariable 3: "
value_three = gets.chomp.to_f

answer = value_one + value_two - value_three
puts "\nIs this correct? \n #{value_one} + #{value_two} - #{value_three} = #{answer}"

system 'pause'

The final document produced should be a PDF or Word document that clearly displays what you are doing in your program – Ruby Example Plan.

Ruby Script Programming – CSV files

Ruby is a dynamic open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. I first learnt RUBY in 2008 when I was introduced to it by a programmer from an agency in Melbourne. I had a skim over the language but thought it’s just like C++ why bother.

Well since being educated about RUBY I’ve come to grips with knowledge of the language supporting multiple OS. In a way it is similar to PHP if you neglect the service side operations rather RUBY enables you to complete similiar if not the same operations except the ability to make system wide changes.

RUBY isn’t platform dependent. Can be used on Linux, Mac OSX and Windows. It’s extendable and programming is a breeze. Here’s an example of a program I made to write to a CSV file that could be used by Excel or an editor capable of displaying CSV files.

CSV file example in RUBY

# Ruby on Rails
# Programmer: Nicholas O'Sullivan, E - nick@autechnet.com
#
# Date Modified - 19 July 2010
# Version: 2.0
# Program Name: CSV File Editor / Creator
# Purpose: To take user input and create a CSV file based upon the input.
#

puts "Your inputs are being recorded\nHello World, I'll ask you for some information now.\n\n"

puts "Your First Name: "
fname = gets.chomp.to_s # 'Chomp' means we accept all the input up until the enter key is pressed.
puts "Your Lastname: "
lname = gets.chomp.to_s # Here's their lastname being asked.

puts "\n\nHello #{fname}, #{lname}, How are you today? (Good (1), Happy (2), Excellent (3),"
puts "Feeling Awesome(4))\nHint - Type the number as the answer (number)\n"

puts "Answer: "
answer = gets.chomp.to_s # Get there response.

if answer == "1"
puts "That's great to hear you are going good\n\n"
elsif answer == "2"
puts "Hi there.. That is great to hear.\n\n"
elsif answer == "3"
puts "That's great to hear you are happy. Townsville is a good place isn't it..\n\n"
elsif answer == "4"
puts "Yeah, isn't it just great when things go excellently. \n\n"
elsif answer == "5"
puts "I believe that that term should only be used to describe God"
puts "as back in the time of Abraham describing God.\n\n"
else
puts "Sorry, I can't work out a suitable response to that answer."
end

system 'pause'
system 'cls'

# Create a new file and write to it
File.open('analysis.csv', 'a+') do | f2 |
# use "\n" for two lines of text
f2.puts "#{fname}, #{lname}, #{answer}\n"
f2.close
puts "\nHas been successfully written.\n"
end
system 'pause'
system 'cls'
system 'pause'

Well that’s about a 20 minuite job just because of the time it takes to write the script. Did I not mention RUBY is a scripting language. This means it can be called during the windows login processes or MACOSX login processes.