MudBytes
» MUDBytes Community » Language Discussions » Ruby » learning ruby, having minor i...
Pages: << prev ... 2, 3, 4, 5, 6 next >>
learning ruby, having minor issues
David Haley
Wizard






Group: Members
Posts: 5,730
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#46 Posted Jul 17, 2009, 6:27 pm

gVim works on all platforms... :wink:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#47 Posted Jul 17, 2009, 8:35 pm

I guess why I can't really empathize is because I don't use a fancy IDE. 
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Idealiad
Sorcerer




Group: Members
Posts: 258
Joined: Jan 28, 2007

Go to the bottom of the page Go to the top of the page
#48 Posted Jul 17, 2009, 9:05 pm


Chris Bailey said:
SciTe makes a good "lite" editor in windows, and there is always NetBeans.


I second a recommendation of SciTe for Windows users, it's what I use for Python and I think it's excellent. Also for Windows I think there's a separate file browser plugin, which I don't use, but puts it between the lite-editor and IDE option.

Igabod
Wizard






Group: Members
Posts: 969
Joined: Jul 23, 2008

Go to the bottom of the page Go to the top of the page
#49 Posted Jul 18, 2009, 12:29 am

I am using SciTe since it's what came with the ruby download.

I realized after all that business with the cygwin debate that I don't even need it, but whatever. No harm no foul I guess.

Ok so I couldn't sleep today for some reason so I got to work on writing a program in ruby that does the 99 bottles of beer on the wall song. I'm kinda proud of it cause I did it without looking at any tutorials or anything. So I felt like posting it on here and getting some input on it.

Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
#count set to 10 for the abbreviated version, just uncomment the count = 99 below and comment out the other one for the full version.
#count = 99
count = 10
command = ' '
 
while count != 0
  puts count.to_s+ ' bottles of beer on the wall, ' +count.to_s+ ' bottles of beer!'
  count = count - 1
  puts 'You take one down and pass it around, ' +count.to_s+ ' bottles of beer on the wall.'
  sleep 1
  puts ' '
end
puts 'No more beer on the wall, No more beer.'
puts 'The guys fall down, I stagger around. No more beer to be found.'
puts ' '
 
while command.downcase != 'quit'
  puts 'Please type quit to exit this program.'
  command = gets.chomp
end
 


One question I had is, is there a ruby equivalent of the C \r\n ? cause it kinda sucks having to put extra lines of code for just a blank line. I'd like to be able to puts 'The next line is a blank line\r\n' using only one \r\n since puts throws one in there anyway. I know the \ is the escape character for ruby too, but when I tried \r\n it just displayed the \r\n instead of giving a new line.
.........................
http://www.dark-warriors.net/banners/sm/5.gif Join the rest of the Dark Warriors in the struggle to be the greatest.

Bored?? Click THIS and you too can build your own mini city.

Every man has his follies, and often they are the most interesting thing he has got - Josh Billings

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein

I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger

Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan

I like long walks, especially when they are taken by people who annoy me. - Fred Allen

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#50 Posted Jul 18, 2009, 12:45 am

Either way irb isn't going to display it like you are expecting on return values.    But here's the answer to the your question:

Code (text):
1
2
3
4
5
6
7
 
"\r\n" ### expands escapes, subs, format str, etc
 
'\r\n'  ### does not expand and is literal
 


To make it actually print it instead of just str.inspect from return in irb....use print "str" or puts "str"
.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Last edited Jul 18, 2009, 12:46 am by Runter
Metsuro
Magician




Group: Members
Posts: 90
Joined: Jan 3, 2007

Go to the bottom of the page Go to the top of the page
#51 Posted Jul 18, 2009, 12:45 am

I made a testing2.rb with one line puts "test\r\ntest2" and did ruby testing2.rb and got...

Code (text):
1
2
3
4
5
6
7
[Sat Jul 18 04:44:39 Strakc@li101-39:~ ] $ ruby testing2.rb
test.
test2
[Sat Jul 18 04:44:45 Strakc@li101-39:~ ] $
 
.........................
http://archive.userbars.com/ub1984906.png

Runter
Wizard






Group: Members
Posts: 1,074
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#52 Posted Jul 18, 2009, 12:50 am

I also don't mind seeing...I guess I got used to it in C++.  Probably could make a singleton for endl if you really wanted to. :P

ENDL = "\r\n"

print "ROAR!!!" + ENDL



.........................
-Heath

For once you have tasted flight Ruby you will walk the earth with your eyes turned skywards,
for there you have been and there you will long to return. --
                                              Leonardo Da Vinci Yukihiro Matsumoto

Igabod
Wizard






Group: Members
Posts: 969
Joined: Jul 23, 2008

Go to the bottom of the page Go to the top of the page
#53 Posted Jul 18, 2009, 1:26 am

Ok, I see so I just need to use the double quotes instead of single quotes when I want to do \r\n\r\n nice. Thanks.

Anybody have any critiques of my 99 bottles program? Anything I can do better or that I didn't need to do? Keeping in mind that I'm still working my way through a basic tutorial for beginners.
.........................
http://www.dark-warriors.net/banners/sm/5.gif Join the rest of the Dark Warriors in the struggle to be the greatest.

Bored?? Click THIS and you too can build your own mini city.

Every man has his follies, and often they are the most interesting thing he has got - Josh Billings

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein

I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger

Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan

I like long walks, especially when they are taken by people who annoy me. - Fred Allen

Metsuro
Magician




Group: Members
Posts: 90
Joined: Jan 3, 2007

Go to the bottom of the page Go to the top of the page
#54 Posted Jul 18, 2009, 1:28 am

the while thing seems odd, I remember a different way to do the loop its like .foreach or something?
.........................
http://archive.userbars.com/ub1984906.png

Igabod
Wizard






Group: Members
Posts: 969
Joined: Jul 23, 2008

Go to the bottom of the page Go to the top of the page
#55 Posted Jul 18, 2009, 2:16 am

what's so odd about it? it says while the count doesn't equal 0 send the first message, reduce the count by one, then send the 2nd message. It's pretty much the same as it would be in C.
.........................
http://www.dark-warriors.net/banners/sm/5.gif Join the rest of the Dark Warriors in the struggle to be the greatest.

Bored?? Click THIS and you too can build your own mini city.

Every man has his follies, and often they are the most interesting thing he has got - Josh Billings

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein

I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger

Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan

I like long walks, especially when they are taken by people who annoy me. - Fred Allen

Idealiad
Sorcerer




Group: Members
Posts: 258
Joined: Jan 28, 2007

Go to the bottom of the page Go to the top of the page
#56 Posted Jul 18, 2009, 2:38 am

I don't know if this is thought of the same way in Ruby, but it could be considered bad practice to use a while when you are iterating over a fixed array size anyway.

Chris Bailey
Sorcerer






Group: Members
Posts: 499
Joined: Sep 13, 2008

Go to the bottom of the page Go to the top of the page
#57 Posted Jul 18, 2009, 2:42 am

There is nothing wrong with while in particular, personally for that I would use downto, which is kind of neat.
Code (text):
1
2
3
4
5
 
99.downto(0) {|i| puts i}
 
.........................
If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.

Quote:
[ichat] Twisol@Talon: Do me, do me.

Igabod
Wizard






Group: Members
Posts: 969
Joined: Jul 23, 2008

Go to the bottom of the page Go to the top of the page
#58 Posted Jul 18, 2009, 2:43 am

well this program was suggested by the tutorial as a test so I'm thinking it is okay. The page that suggests it is http://pine.fm/LearnToProgram/?Chapter=06. It's down at the bottom of a page that has just gone over while and if/else statements.
.........................
http://www.dark-warriors.net/banners/sm/5.gif Join the rest of the Dark Warriors in the struggle to be the greatest.

Bored?? Click THIS and you too can build your own mini city.

Every man has his follies, and often they are the most interesting thing he has got - Josh Billings

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein

I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger

Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan

I like long walks, especially when they are taken by people who annoy me. - Fred Allen

Chris Bailey
Sorcerer






Group: Members
Posts: 499
Joined: Sep 13, 2008

Go to the bottom of the page Go to the top of the page
#59 Posted Jul 18, 2009, 2:55 am

This might contain some useful things for you to try out. Maybe =P
Code (text):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
class BeerSinger
  attr_reader :count
  def initialize count
    @count = count
  end
 
  def sing_line
    if @count > 0
      puts "#@count bottles of beer on the wall, #@count bottles of beer!\r\n"
      remove_beer
      puts "You take one down and pass it around, #@count bottles of beer on the wall.\r\n"
      sleep 1
    else
      puts "No more beer on the wall, No more beer.\r\n"
      puts "The guys fall down, I stagger around. No more beer to be found.\r\n"
      sleep 1
    end
  end
 
  def remove_beer
    @count -= 1
  end
end
 
puts "How many beers are on the wall?"
bs = BeerSinger.new(gets.chomp.to_i)
bs.count.downto(0) {bs.sing_line}
 
.........................
If what Proust says is true, that happiness is the absence of fever, then I will never know happiness. For I am possessed by a fever for knowledge, experience, and creation.

Quote:
[ichat] Twisol@Talon: Do me, do me.

Igabod
Wizard






Group: Members
Posts: 969
Joined: Jul 23, 2008

Go to the bottom of the page Go to the top of the page
#60 Posted Jul 18, 2009, 3:36 am

defining remove_beer seems kinda pointless to me since it's only one line and is only used once in the program. And why set the class and all that stuff at the beginning?

Some of that stuff is a little bit more advanced than my skill is but my familiarity with C allows me to know what it's doing (somewhat). But I'm trying to stick with doing these programs using only the stuff the tutorial has taught me so far. Once I finish with this tutorial I'll begin trying to do the same programs in different ways, but till then I just wanna learn how to do the basic stuff.

Though looking over your version of that program again, I like the if method better than the while method for this type of thing. Your program has more lines of actual code in it than mine though with all that extra stuff at the beginning and the un-needed def of remove_beer. If I condense my program down, removing un-needed blank lines and commented out lines, I am using 14 lines. Seems more efficient this way. Is there some reason more efficient isn't a good thing in this case?

P.S. You did give me a few ideas on how to make some of my other piddly little programs better though, thank you for that.
.........................
http://www.dark-warriors.net/banners/sm/5.gif Join the rest of the Dark Warriors in the struggle to be the greatest.

Bored?? Click THIS and you too can build your own mini city.

Every man has his follies, and often they are the most interesting thing he has got - Josh Billings

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. - Albert Einstein

I have a love interest in every one of my films... a gun. - Arnold Schwarzenegger

Recession is when a neighbor loses his job. Depression is when you lose yours. - Ronald Reagan

I like long walks, especially when they are taken by people who annoy me. - Fred Allen

Pages:<< prev ... 2, 3, 4, 5, 6 next >>

Valid XHTML 1.1! Valid CSS!