Ruby

Post anything related to programming or technology here. (Topics can be bumped at any time)
Forum rules
View them here. Make sure you read and agree to abide by the rules before posting.

Re: Ruby

Postby DarkFlame on Tue Jul 21, 2009 12:49 am

stykzman wrote:I think what might help is some examples of each, like "here's how you'd do *this* in Python, and here's how you'd do the same thing in Ruby"...

:stykz:

again, I agree.


So I'll start off with the classic:
Code: Select all
>>>print "Hello World!"
Hello World!

This is what the interactive interpreter would do. (Code blocks are after >>>, output is plain text.)
How does Ruby handle that?
Image
User avatar
DarkFlame
* Most Helpful Person 2010 *
 
Posts: 3038
Joined: Sat Feb 21, 2009 2:43 pm

Re: Ruby

Postby ReecyBoy on Tue Jul 21, 2009 11:04 pm

For Ruby, one would write:
Code: Select all
puts "Hello World!"

and the output would be:
Code: Select all
Hello World!


If one were to enter:
Code: Select all
print "Hello World!"

into Ruby, the output would be:
Code: Select all
Hello World!=> nil
Currently running: Stykz Version 1.0 RC3 (194)
Image
ImageImage
Image
Image
Image
DarkFlame wrote:It looks like someone burnt a hole in reality (Which happens to be barf green..) and saw another dimension on the other side.
User avatar
ReecyBoy
* Best Stykfigure Maker 2010 *
 
Posts: 184
Joined: Mon Feb 09, 2009 3:53 am
Location: <insert mildly amusing location name here>

Re: Ruby

Postby stykzman on Wed Jul 22, 2009 12:20 am

ReecyBoy wrote:For Ruby, one would write:
Code: Select all
puts "Hello World!"

and the output would be:
Code: Select all
Hello World!


If one were to enter:
Code: Select all
print "Hello World!"

into Ruby, the output would be:
Code: Select all
Hello World!=> nil

OK, and Revolution would do:
Code: Select all
answer "Hello World!"

But how about something a bit more significant? Like some text parsing? Say you had a text string and you needed to count the number of words in a sentence. How would you do that in Python and Ruby?

:stykz:
:stykz:
User avatar
stykzman
Site Admin
 
Posts: 1870
Joined: Wed Jul 30, 2008 10:59 pm

Re: Ruby

Postby DarkFlame on Wed Jul 22, 2009 12:47 am

In python:
Code: Select all
len("Hello world!")

would output:
Code: Select all
12
Image
User avatar
DarkFlame
* Most Helpful Person 2010 *
 
Posts: 3038
Joined: Sat Feb 21, 2009 2:43 pm

Re: Ruby

Postby stykzman on Wed Jul 22, 2009 2:44 am

DarkFlame wrote:In python:
Code: Select all
len("Hello world!")

would output:
Code: Select all
12

That's the number of *characters*, not the number of *words*. The output should be "2" in this case... so how would you determine the number of words in Python?

:stykz:
:stykz:
User avatar
stykzman
Site Admin
 
Posts: 1870
Joined: Wed Jul 30, 2008 10:59 pm

Re: Ruby

Postby DarkFlame on Wed Jul 22, 2009 2:57 am

stykzman wrote:
DarkFlame wrote:In python:
Code: Select all
len("Hello world!")

would output:
Code: Select all
12

That's the number of *characters*, not the number of *words*. The output should be "2" in this case... so how would you determine the number of words in Python?

:stykz:

I would personally use Regular expressions to find all the spaces, but I'm too tired to think about REs right now.
Image
User avatar
DarkFlame
* Most Helpful Person 2010 *
 
Posts: 3038
Joined: Sat Feb 21, 2009 2:43 pm

Re: Ruby

Postby ReecyBoy on Wed Jul 22, 2009 3:23 am

Look, to tell the truth, I'm a real Ruby n00b. So I have no idea how one would accomplish this. I can get Ruby to count the number of characters, I think. I saw it written somewhere, so I'm pretty sure. But a quick Google search has revealed many different confusing methods to accomplish the counting of words.
Currently running: Stykz Version 1.0 RC3 (194)
Image
ImageImage
Image
Image
Image
DarkFlame wrote:It looks like someone burnt a hole in reality (Which happens to be barf green..) and saw another dimension on the other side.
User avatar
ReecyBoy
* Best Stykfigure Maker 2010 *
 
Posts: 184
Joined: Mon Feb 09, 2009 3:53 am
Location: <insert mildly amusing location name here>

Re: Ruby

Postby Chris on Wed Jul 22, 2009 5:21 am

Is Ruby th Ruby on rails thing?
(:
User avatar
Chris
* Most Active Member 2010 *
 
Posts: 1549
Joined: Mon Jan 05, 2009 11:31 pm
Location: Iowa

Re: Ruby

Postby DarkFlame on Wed Jul 22, 2009 11:32 am

ok nevermind, I found how to do it in Python:

Code: Select all
import string
words = string.split("Hello World!")
len(words)


The output would be:
Code: Select all
2
Image
User avatar
DarkFlame
* Most Helpful Person 2010 *
 
Posts: 3038
Joined: Sat Feb 21, 2009 2:43 pm

Re: Ruby

Postby stykzman on Wed Jul 22, 2009 3:39 pm

DarkFlame wrote:ok nevermind, I found how to do it in Python:

Code: Select all
import string
words = string.split("Hello World!")
len(words)


The output would be:
Code: Select all
2

Cool... it looks like you don't need to provide a character to split on; Python assumes a space unless you provide a delimiter, right?

:stykz:
:stykz:
User avatar
stykzman
Site Admin
 
Posts: 1870
Joined: Wed Jul 30, 2008 10:59 pm

PreviousNext

Return to Programming & Tech

Who is online

Users browsing this forum: No registered users and 1 guest

cron