Difference between 11_223 and 11223? [duplicate] - ruby

This question already has answers here:
What does the underscore mean in literal numbers?
(4 answers)
Closed 8 years ago.
What is difference and why did someone write number with underscore?
irb(main):001:0> a = 11_223
=> 11223
irb(main):002:0> b = 11223
=> 11223
irb(main):003:0> a == b
=> true
irb(main):004:0> a === b
=> true
irb(main):005:0> 11_223 === 11223
=> true

When you write 1 million in numbers, you would usually do:
1,000,000
and not:
1000000
To make it more readable.
You can do the same thing in Ruby with an underscore:
1_000_000
Ruby can't use the ,, because that's already used in other things (like function arguments), so the "strange" underscore character is used.

Related

why there is a column ":" in front of key in ruby [duplicate]

This question already has answers here:
Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
(5 answers)
Closed 1 year ago.
aaa = { :one => "eins", :two => "zwei", :three => "drei" }
bbb = { one: "eins", two: "zwei", three: "drei" }
above are valid ruby code. at line 1, why there is ":" before "one"? What's the meaning?
It's called Symbol, you could think of it as a special string. Symbols are often used as hash keys.
You could check out more on Ruby Symbols vs. Strings
In ruby hash is an association of key and value.
my_hash = { :key => "value" }
or
my_hash = { key: "value" }
more informations here : https://launchschool.com/books/ruby/read/hashes

ruby interpreter: when is a variable defined? [duplicate]

This question already has answers here:
Why can I refer to a variable outside of an if/unless/case statement that never ran?
(3 answers)
Closed 5 years ago.
I noticed a pretty odd behaviour in a ruby irb console. If I execute:
irb(main):001:0> defined?(a)
=> nil
irb(main):002:0> a = true if defined?(a)
=> true
irb(main):003:0> a
=> true
since I haven't defined a and defined?(a) returns false I would expect a = true to not be executed. But that's not the case and a = true is actually executed and a has value true.
This can be simplified to
irb(main):001:0> a = "hello" if false
=> nil
irb(main):002:0> defined?(a)
=> "local-variable"
What I thought at the beginning is that a is defined before checking the condition but converting this into:
irb(main):001:0> (a = "hello") if false
=> nil
irb(main):002:0> defined?(a)
=> "local-variable"
Who can explain this behaviour to me? How does ruby evaluate such statement?
if false
a = "hello"
end
defined?(a) # true
It has nothing to do with defined?. It seems the parser needs to define that variable in advance if there's a chance it needs that. Check this
a
NameError (undefined local variable or method `a' for main:Object
a = 4 if false
nil
a
nil

colon placement in Ruby 2.2+ [duplicate]

This question already has answers here:
Is there any difference between the `:key => "value"` and `key: "value"` hash notations?
(5 answers)
Closed 7 years ago.
I see colons used two different ways in Ruby
:controller => 'pages'
and then
action: => 'home'
I found an explanation here: http://goo.gl/ZKxKVK
it seems that the position doesn't matter, could someone clarify this?
Mostly it doesn't matter. Since Ruby 1.9 we can use more short form:
h = { a: 1, b: 2}
But there are some situations where you have to use the longest form, e.g.:
h = {1 => 'a', 2 => 'b'}
h = {"One Two" => 1}
action: => 'home' is not valid syntax.
It should be action: 'home' or :action => 'home'.
These are equivalent. They generate:
{:action=>'home'}

Ruby: How come the same strings have different hashcodes?

test = 'a'
test2 = '#a'.slice(0)
test3 = '#a'[1]
puts test.hash
puts test2.hash
puts test3.hash
Output:
100
64
97
Is this a bug or am I misunderstanding how the hash method works? Is there a way to fix this?
The results of these expressions are not all the same data. Ruby 1.8 integers contain character numbers for single character indexing. This has been changed in Ruby 1.9, but slice(0) returns the first character of the string '#', not 'a'.
In Ruby 1.8 (using irb):
irb(main):001:0> test = 'a'
=> "a"
irb(main):002:0> test2 = '#a'.slice(0)
=> 64
irb(main):003:0> test3 = '#a'[1]
=> 97
irb(main):004:0> test.hash
=> 100
irb(main):005:0> test2.hash
=> 129
irb(main):006:0> test3.hash
=> 195
In Ruby 1.9.1:
irb(main):001:0> test = 'a'
=> "a"
irb(main):002:0> test2 = '#a'.slice(0)
=> "#"
irb(main):003:0> test3 = '#a'[1]
=> "a"
irb(main):004:0> test.hash
=> 1365935838
irb(main):005:0> test2.hash
=> 347394336
irb(main):006:0> test3.hash
=> 1365935838
The reason is that each variable refers to different a object with its own unique hash code! The variable test is the string "a", test2 is the integer 64 (the character number of '#'), and test3 is the integer 97 ('a'). The surprise is probably that in Ruby, the elements of strings are integers, not strings or characters.
As maerics points out, unless you've defined your own hash method for the class you're using, the hash might simply be on the object itself, not its contents. That said, you can (and should) define your own hash method for any class where you define an equals method.
In Ruby, the String class already does this for you:
irb(main):001:0> test="a"
=> "a"
irb(main):002:0> test2="a"
=> "a"
irb(main):003:0> test.hash
=> 100
irb(main):004:0> test2.hash
=> 100
irb(main):005:0> test2[0]=test.slice(0)
=> 97
irb(main):006:0> test2
=> "a"
irb(main):007:0> test2.hash
=> 100
I haven't found an equivalent text for Ruby, but this page on Java gives an excellent algorithm for generating your own hash code that's not hard to copy for Ruby: http://www.javapractices.com/topic/TopicAction.do?Id=28

ruby double question mark [duplicate]

This question already has answers here:
what is "?" in ruby
(3 answers)
Closed 7 years ago.
I came across this piece of ruby code:
str[-1]==??
What is the double question mark all about? Never seen that before.
Ruby 1.8 has a ?-prefix syntax that turns a character into its ASCII code value. For example, ?a is the ASCII value for the letter a (or 97). The double question mark you see is really just the number 63 (or the ASCII value for ?).
?a # => 97
?b # => 98
?c # => 99
?\n # => 10
?? # => 63
To convert back, you can use the chr method:
97.chr # => "a"
10.chr # => "\n"
63.chr # => "?"
??.chr # => "?"
In Ruby 1.9, the ?a syntax returns the character itself (as does the square bracket syntax on strings):
?? # => "?"
"What?"[-1] # => "?"
As Ryan says, the ? prefix gives you the ASCII value of a character. The reason why this is useful in this context is that when you use the index notation on a string in Ruby 1.8 the ASCII value is returned rather than the character. e.g.
irb(main):009:0> str = 'hello'
=> "hello"
irb(main):010:0> str[-1]
=> 111
so the following wouldn't test if the last character of a string was the letter 'o'
irb(main):011:0> str[-1] == 'o'
=> false
but this would:
irb(main):012:0> str[-1] == ?o
=> true
and (provided you know what the ? does!) this is slightly clearer than
irb(main):013:0> str[-1] == 111
=> true

Resources