Author Topic: [Ruby] MyFirst program  (Read 1810 times)

0 Members and 4 Guests are viewing this topic.

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
[Ruby] MyFirst program
« on: October 23, 2012, 05:22:27 pm »
So guys, its my first program in ruby.
Code: [Select]
print  "===========================================================\n"
print "============ Simple Calculator Menu=========================\n"
print "============================================================="
print "\n\n"
puts " 1.)Addition       \t 2.)Subtraction \n 3.)Multiplication \t 4.)Division \n 5.)Power          \t 6.)Square Root"
c=Integer(gets.chomp)
if c == 1
  print "Enter any integer:  "
a=Integer(gets.chomp)
print "Enter another integer: "
b=Integer(gets.chomp)
d=a+b
print "The sum is: "+d.to_s
elsif c == 2
  print "Enter any integer: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a-b
print "The difference is: "+d.to_s
elsif c == 3
  print "Enter a number: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a*b
print "The product is "+d.to_s
elsif c == 4
  print "Enter a number: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a/b
print "The quotient after dividing "+a.to_s+" by "+b.to_s+" is "+d.to_s
elsif c == 5
  print "Enter the number: "
  a=Integer(gets.chomp)
  print "Enter the power: "
  b=Integer(gets.chomp)
  d=a**b
  print ""+a.to_s+" raised to power "+b.to_s+" is "+d.to_s
 elsif c == 6
   print "Enter the number: "
   a=Integer(gets.chomp)
   d=a**0.5
   print "The square root of "+a.to_s+" is "+d.to_s
   else
     print "Wrong choice"
   
end


Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Ruby] MyFirst program
« Reply #1 on: October 23, 2012, 06:12:27 pm »
First of all, this is not a "program", it's a Script.
Second of all, it's acceptable as a first script. But if you keep up with such code formatting (ugly as hell) it will not be acceptable and people here won't take it nicely.

I am sure Ruby, as every other language, has specific Ruby conventions, but general code conventions apply to every (real) coding language.

One of your mistakes are missplaced spaces,
bad indentation,
lack of spaces (b=Integer(gets.chomp) ;; d=a+b),
variable names ar horrible - use words instead of letters,
no grouping for logical statements (elsif c == 5)...

This is acceptable as a working (I assume it's working, can't run it) solution. Correct the errors I pointed out and you'll do much better.

Waiting for your other scripts :)

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [Ruby] MyFirst program
« Reply #2 on: October 24, 2012, 10:13:28 am »

Quote
variable names ar horrible - use words instead of letters,[/size]
I have used appropriate symbols.
c for choice, a for first input(as it is the first alphabet of English language),b for second input(the reason is same as that in case of a),now after that c is chosen therefore I used d to store the result.It is very simple and compact and no one should have problem with it.
Quote
[/size]no grouping for logical statements (elsif c == 5)...[/color][/size]
I recently started learning ruby language and therefore it is correct.

Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [Ruby] MyFirst program
« Reply #3 on: October 24, 2012, 10:32:46 am »
*sight* some people should not post at all...

You clearly did not understand what I was trying to explain, but I don't care anymore... also the colors in your post does not make it any better to read. If you're going to post like such, at least don't make mistakes with formatting and stuff...
« Last Edit: October 24, 2012, 10:33:49 am by Kulverstukas »

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [Ruby] MyFirst program
« Reply #4 on: October 24, 2012, 12:00:14 pm »
*sight* some people should not post at all...

You clearly did not understand what I was trying to explain, but I don't care anymore... also the colors in your post does not make it any better to read. If you're going to post like such, at least don't make mistakes with formatting and stuff...
I used the different colors to highlight what I want to say.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Ruby] MyFirst program
« Reply #5 on: October 25, 2012, 10:28:03 am »
quick rewrite, might help

Code: (ruby) [Select]
# ruby has puts(print + newline), use it
# also has heredocs
puts <<EOS
================================================
============ Simple Calculator Menu ============
================================================

1) Addition
2) Substraction
3) Multiplication
4) Division
5) Exponent
6) Root

EOS

# AFAIK ruby doesn't have a prompt/stdin get
# ARGF(args/stdin) is the way to go anyways
# programs that ask for input usually suck
def get(prompt)
    print prompt
    STDOUT.flush
    gets.chomp
end

# until is between 1 - 6
until (opt = get('enter option: ').to_i).between?(1,6)
  puts "unknown option: #{opt}"
end

a = get("first number: ").to_f
b = get("second number: ").to_f

# use switch/case
result = case opt
  when 1 then a + b
  when 2 then a - b
  when 3 then a * b
  when 4 then a / b
  when 5 then a**b
  when 6 then a**(1/b)
end

puts "result: #{result}"

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: [Ruby] MyFirst program
« Reply #6 on: October 25, 2012, 12:59:26 pm »
I have used appropriate symbols.
c for choice, a for first input(as it is the first alphabet of English language),b for second input(the reason is same as that in case of a),now after that c is chosen therefore I used d to store the result.It is very simple and compact and no one should have problem with it.I recently started learning ruby language and therefore it is correct.


If you didn't wish to ask for opinions, why post the code?
"Always have a plan"