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

Tags:

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

Professional Services

Contemplating an upgrade or refresh of your IT infrastructure? If so, comprehensive planning is a critical determinant of success. How do you know which of the myriad of new technology solutions available will best equip you to harness greater efficiencies and reduce costs? And how will you go about demonstrating return on your investment? At Au Technet, through our professional services, we support you in the planning and building of new solutions for your business. Drawing on our focus and several decades of experience, we’re quickly able to recognise and understand evolving technologies as they emerge and assist you to convert the most appropriate ones into solutions that deliver measurable value to your business. Every year we assistclients across Australia to understand exactly how new solutions will fit into their current environment and move forward with investments and deployments with confidence.

Contemplating an upgrade or refresh of your IT infrastructure? If so, comprehensive planning is a critical determinant of success. How do you know which of the myriad of new technology solutions available will best equip you to harness greater efficiencies and reduce costs? And how will you go about demonstrating return on your investment? At Dimension Data, through our professional services, we support you in the planning and building of new solutions for your business. Drawing on our focus and several decades of experience, we’re quickly able to recognise and understand evolving technologies as they emerge and assist you to convert the most appropriate ones into solutions that deliver measurable value to your business. Every year we assist thousands of clients across the globe to understand exactly how new solutions will fit into their current environment and move forward with investments and deployments with confidence.

Planning a Program

Tags: ,

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.