Jump to content

Can you read Ruby?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
In my opinion Ruby is an extremely readable language. I made a number guessing game as my first program in Ruby:
class GuessGame

    def initialize

        @correct = rand(1000) + 1 # Correct guess.

        @max = 1000 # Maximum and minimum for

        @min = 1    # "homing in" on the answer.

        @last = nil # The last guess.

    end

    def guess (x)

        curr = (x - @correct).abs

        if @last

            lastd = (@last - @correct).abs 

            if lastd < curr

                print "Colder\n"

            elsif lastd > curr

                print "Warmer\n"

                if x > @last && @last > @min 

                    @min = @last # Increase the minimum.

                elsif x < @last && @last < @max

                    @max = @last # Decrease the maximum.

                end  

            end

            if x > @max || x < @min

                print "Idiot!\n" # Problem exists between keyboard and chair.

            end 

        end

        @last = x; # This guess becomes the last guess

    end

    attr_reader :max, :min

end


game = GuessGame.new

continue = true # Continue until the user exits.

while(continue)

    print "Guess: " # Prompt.

    cmd = gets 

    if cmd =~ /exit|quit|bye/i # Exit commands; case insensitive.

        print "See ya!\n"

        exit

#   elsif cmd =~ /max|min/i # For testing.

#       print game.max, "\n", game.min, "\n"

    elsif cmd =~ /^[0-9]+/

        game.guess cmd.to_i

    end 

end

Do you think it's readable?

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Yes, it's pretty readable, but also ugly as hell. I have never liked its syntax, and never will.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's extremely readable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
It seems like neat code, but I have never actually had anything to do with Ruby so it looks all confusing to me.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I did a bit of programming in Ruby and one the main things that turned me off was the optional ending syntax. Like v0id said, it is ugly as hell.

#6
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Hmmmm...... I know to stay away from Ruby then hehe
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#7
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts

Jordan said:

I did a bit of programming in Ruby and one the main things that turned me off was the optional ending syntax. Like v0id said, it is ugly as hell.
Think of it like shell scripts: you only need a semicolon when you put more than one statement in a line. Personally I like the syntax because it doesn't need as much ****. By ****, I mean {} and (). Those two are the main reason why code in languages like C is unreadable.

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I suppose it is just like coding shell scripts. I never looked at it from that angle before.