2 Replies - 1563 Views - Last Post: 19 October 2008 - 10:55 AM Rate Topic: -----

#1 Seraphless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-June 08

A few questions

Post icon  Posted 06 June 2008 - 09:37 AM

Is there a difference in execution speed between:
if something print('something') #and
print('something') if something

-------------------------------------------------------------------------

Why is it that when you invoke the commands:
gets.chomp!.reverse #the input is reversed
gets.reverse.chomp! #the input prints normal
gets.reverse!.chomp #the input is reversed

-------------------------------------------------------------------------

Am I correct in thinking that '|x, y|' in relation to a hash/dictionary creates a sort of template instructing Ruby to recognize the pattern and assign the variables accordingly? i.e. |key, value|

How does that setup apply to other types?

What is the definition of 'each' in relation to each type. Is this method of assigning variables similar to the 'for' command in Python? 'Do'?

-------------------------------------------------------------------------

What is the difference between strip and chomp?

-------------------------------------------------------------------------

If `ls` sends ls to the terminal, why doesn't `cd ~/` change my directory while in Ruby?

-------------------------------------------------------------------------

Why are Kernel.open, Kernel::open, File.open, and File::open seemingly synonymous? Is one better than another? Faster?

-------------------------------------------------------------------------

Why is it that when a single string index is called, it returns the code for the letter/symbol in question, yet calling the entire string via [0..X] yields the actual string?

-------------------------------------------------------------------------

+

Is This A Good Question/Topic? 0
  • +

Replies To: A few questions

#2 Inkman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-October 08

Re: A few questions

Posted 15 October 2008 - 12:02 PM

Those look like really good questions, I hope someone answers them.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: A few questions

Posted 19 October 2008 - 10:55 AM

1) There is no difference between having the if statement up front or as a modifier on the end. They are just syntax differences and both are the same. Get use to it, Ruby has variations for everything that are just aliases for other commands. I kinda hate it myself.

2) The trick here is that exclamation mark which says that it will edit the string in place instead of making a new string or return nil if no changes were made. So if you take a string, reverse it (like in the second one which now puts the carriage return on the front) when you go to chomp with chomp! there is no carriage return to whack off, so your second one doesn't return a normal string it should return "nil". The third one, since it is not using chomp! (in place) it just does the same as the second case but will return the reversed string instead of nil, even though it has made no changes.

3) I am not quite sure what you are asking here, but in a hash the object you specify first is used as a key and the second is the value. Now if you are talking about a block where you have |x,y| then all it is doing is simply taking the key and sticking it in "x" and the value in "y" so that you can then use them in a block. This would make "x" and "y" take on the objects of those in the hash/dictionary. I guess in a subtle way it could be like a template in STL of C++ would be, but it is more like a simple assignment of an object to a variable of data type Object.

4) How does that setup apply to other types? Same thing, Ruby is not strongly typed and will analyze the object and assign the value accordingly based on that type. Its much like PHP in a way and would make sense since both PHP and Ruby are interpreted languages.

5) I am not sure about python, but the each method is simply an iterator which yields the current object pointed to to a block that usually is given to the the method. And like stated in answer 3 above, this object is passed to the block as a parameter in a "yield" call and those objects are assigned to the variables in between the pipes.

# Define "a" to be a range from 0 to 3
a = 0..3

# Use the each to loop through the range, passing each object to our block
a.each { |x| puts x }

# Results
0
1
2
3



This is because the each method for each type is iterating through the collection, passing each one to the |x| we defined in our block. This means that the each method must be given a block and it checks by using "block_given?" which returns true or false to know if a block is provided.

6) Strip trims whitespace from the start AND the end, chomp removes from the end of a string and can take a parameter to specify a parameter other than the default. Example... "hello".chomp("llo") => "he".

7) Are you sure that cd ~/ works? If you are on windows this is a unix syntax, if you are on unix see if you can change to another directory using the tilde. If you are trying to return home you can try cd with no operands.

8) :: is a scope resolution operator. This means that it is used to access variables, objects, and methods that are outside the current scope by referencing the class name. The method "open" of file is what you would call "static" in other languages. It doesn't need an instance of File to be used. Almost always you see File.open rather than File::open both both will work all the same. There is no difference in this situation.

9) The answer to your last question is right in the help under the [] operator for string. Oh yes, [] is an operator like +, -, =, == etc. In the documentation they have specified that if you provide a single index to this operator and it will return a fixednum code for the character. If you specify a range ([0..3] is a range if you didn't know) then it would return a string or nil. This is just how they implemented the operator. If you don't believe it should be like that, you can override the [] operator yourself. But I will leave that up to you.

Hope these were good enough answers. Next time though, you might want to ask one or two questions when you come across them rather than collect them all and save them. Not many of us want to sit here and answer a bajillion questions at once. We have others to help too and like to spread around our time.

Thanks!

"At DIC we be ruby stallion code ninjas... if you cut us, we would bleed rubies, I sh*t you not!" :snap:
Was This Post Helpful? 1

Page 1 of 1